【AsyncTask整理 2】 AsyncTask方法代码演示

Android SDK为我们提供了一个后台任务的处理工具AsyncTask。AsyncTask就是一个封装过的后台任务类顾名思义就是异步任务,方便我们维护,Android开发网提示这样的好处可以解决一些线程安全问题,AsyncTask直接继承于Object类,位置为 android.os.AsyncTask。

                 三个泛型:

                 Param ,任务执行器需要的数据类型
                 Progress 后台计算中使用的进度单位数据类型
                 Result 后台计算返回结果的数据类型


Java代码:

 1         public class main extends Activity {
 2           ImageView imageView01;
 3           TextView textView;
 4           @Override
 5           public void onCreate(Bundle savedInstanceState) {
 6             super.onCreate(savedInstanceState);
 7             setContentView(R.layout.main);
 8             textView = (TextView) this.findViewById(R.id.TextView01);
 9             imageView01 = (ImageView) this.findViewById(R.id.ImageView01);
10             GetImage getImage = new GetImage();
11             getImage.execute("http://hi.csdn.net/attachment/201010/27/0_1288149117Yk8W.gif");
12           }
13           private class GetImage extends AsyncTask {
14             public GetImage() {
15             super();
16             // TODO Auto-generated constructor stub
17             }
18           @Override
19           protected void onCancelled() {
20             Log.i("czb", "onCancelled is running...");
21             super.onCancelled();
22           }
23           @Override
24           protected void onPostExecute(Object result) {
25             /*
26              * 此方法在主线程执行,任务执行的结果作为此方法的参数返回
27             */
28             Log.i("czb", "onPostExecute is running...");
29             Log.i("czb", "result == null ? " + (result == null));
30             imageView01.setImageBitmap((Bitmap)result);
31             super.onPostExecute(result);
32           }
33           @Override
34           protected void onPreExecute() {
35             /*
36             * 执行预处理,它运行于UI线程,可以为后台任务做一些准备工作,比如绘制一个进度条控件
37             */
38             Log.i("czb", "onPreExecute is running...");
39             super.onPreExecute();
40           }
41           @Override
42           protected void onProgressUpdate(Object... values) {
43             /*
44             * 此方法在主线程执行,用于显示任务执行的进度。
45             */
46             Log.i("czb", "onProgressUpdate is running...");
47             // 由publishProgress传递的值
48             Log.i("czb", "values " + values[0]);
49             super.onProgressUpdate(values);
50           }
51           @Override
52           protected Object doInBackground(Object... params) {
53             /*
54             * 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。
55             * 在执行过程中可以调用publicProgress(Progress…)来更新任务的进度。
56             */
57             Log.i("czb", "doInBackground is running...");
58             try {
59               Bitmap bitmap;
60               HttpClient client = new DefaultHttpClient();
61               // params[0]代表连接的url
62               URI uri = URI.create((String) params[0]);
63               HttpGet get = new HttpGet(uri);
64               HttpResponse response = client.execute(get);
65               HttpEntity entity = response.getEntity();
66               long length = entity.getContentLength();
67               Log.i("czb", " " + length);
68               InputStream in = entity.getContent();
69               if (in != null) {
70               ByteArrayOutputStream baos = new ByteArrayOutputStream();
71               /*byte[] buf = new byte[128];
72               int ch = -1;
73               int count = 0;
74               while ((ch = in.read(buf)) != -1) {
75               baos.write(buf, 0, ch);
76               count += ch;
77               if (length > 0) {
78               // 如果知道响应的长度,调用publishProgress()更新进度
79               // onProgressUpdate读取进度
80               publishProgress((int) ((count / (float) length) * 100));
81                }
82               // 为了在模拟器中清楚地看到进度,让线程休眠100ms
83               //Thread.sleep(100);
84             }*/
85            bitmap = BitmapFactory.decodeStream(in);
86            in.close();
87            baos.close();
88            return bitmap;
89           }
90          } catch (Exception e) {
91             e.printStackTrace();
92           }
93          return null;
94         }
95       }
96     }

 

posted @ 2014-12-23 15:14  马走日  阅读(205)  评论(0编辑  收藏  举报