Androidの多线程之更新ui(AsyncTask)

AsyncTask

构造(泛型参数)

外部步骤:

AsyncTask task = new AsyncTask<params,values,result>();

task.exec(params);

内部流程:

1.void onPreExecute() ;//里头可放些提醒类的东西

2.result doInBackground(params){publishProgress(values);};//执行任务,取得结果并返回

3.void onProgressUpdate(values);//接收publishProgress()参数,可以更新进度

4.void onPostExecute(result);//执行完成,取得结果,可在UI上展示

  1 package com.bvin.aync;
  2 
  3 import java.io.IOException;
  4 
  5 import org.apache.http.HttpResponse;
  6 import org.apache.http.client.ClientProtocolException;
  7 import org.apache.http.client.HttpClient;
  8 import org.apache.http.client.methods.HttpGet;
  9 import org.apache.http.impl.client.DefaultHttpClient;
 10 
 11 import android.app.Activity;
 12 import android.app.ProgressDialog;
 13 import android.graphics.Bitmap;
 14 import android.graphics.BitmapFactory;
 15 import android.os.AsyncTask;
 16 import android.os.Bundle;
 17 import android.os.Handler;
 18 import android.os.Message;
 19 import android.view.View;
 20 import android.widget.Button;
 21 import android.widget.ImageView;
 22 import android.widget.Toast;
 23 
 24 public class AyncActivity extends Activity {
 25     /** Called when the activity is first created. */
 26     private ImageView iv;
 27     private ProgressDialog dialog;
 28     @Override
 29     public void onCreate(Bundle savedInstanceState) {
 30         super.onCreate(savedInstanceState);
 31         setContentView(R.layout.main);
 32        
 33         initViews();
 34     }
 35     
 36     private void initViews(){
 37         Button bt = (Button)findViewById(R.id.bt);
 38         iv =  (ImageView)findViewById(R.id.iv);
 39         
 40         bt.setOnClickListener(new View.OnClickListener() {
 41             
 42             @Override
 43             public void onClick(View v) {
 44                 // TODO Auto-generated method stub
 45                 loadImages();
 46             }
 47         });
 48         
 49         dialog = new ProgressDialog(this);
 50         
 51     }
 52     
 53     private void loadImages(){
 54         
 55         LoadImageTask task = new LoadImageTask();
 56         task.execute("http://avatar.csdn.net/0/3/4/1_mylzc.jpg");
 57     }
 58     
 59     class LoadImageTask extends AsyncTask<String,Integer,Bitmap>{
 60 
 61         @Override
 62         protected Bitmap doInBackground(String... params) {
 63             // TODO Auto-generated method stub
 64             Bitmap bitmap  = null;
 65             HttpGet get = new HttpGet(params[0]);
 66             HttpClient client = new DefaultHttpClient();
 67             try {
 68                 HttpResponse res = client.execute(get);
 69                 bitmap = BitmapFactory.decodeStream(res.getEntity().getContent());
 70             } catch (ClientProtocolException e) {
 71                 // TODO Auto-generated catch block
 72                 e.printStackTrace();
 73             } catch (IOException e) {
 74                 // TODO Auto-generated catch block
 75                 e.printStackTrace();
 76             }
 77             
 78             
 79             return bitmap;
 80         }
 81 
 82         @Override
 83         protected void onPostExecute(Bitmap result) {
 84             // TODO Auto-generated method stub
 85             super.onPostExecute(result);
 86             dialog.dismiss();
 87             if(result!=null){
 88                 iv.setImageBitmap(result);
 89                 Toast.makeText(AyncActivity.this, "图片下载成功", 1000).show();
 90                 
 91             }else{
 92                 
 93                 Toast.makeText(AyncActivity.this, "图片下载失败", 1000).show();
 94             }
 95             
 96         }
 97 
 98         @Override
 99         protected void onPreExecute() {
100             // TODO Auto-generated method stub
101             super.onPreExecute();
102             dialog.show();
103         }
104 
105         @Override
106         protected void onProgressUpdate(Integer... values) {
107             // TODO Auto-generated method stub
108             super.onProgressUpdate(values);
109         }
110 
111         
112         
113         
114         
115     }
116 }

转载于:https://www.cnblogs.com/bvin/archive/2012/11/22/2783070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值