AsyncTask异步任务使用解析

来源:互联网 发布:手机 gba模拟器 知乎 编辑:程序博客网 时间:2024/04/19 16:57

AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用。

AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法。注意继承时需要设定三个泛型Params,Progress和Result的类型,如AsyncTask<Void,Inetger,Void>

  • Params是指调用execute()方法时传入的参数类型和doInBackgound()的参数类型
  • Progress是指更新进度时传递的参数类型,即publishProgress()和onProgressUpdate()的参数类型
  • Result是指doInBackground()的返回值类型
上面的说明涉及到几个方法:
  • doInBackgound()这个方法是继承AsyncTask必须要实现的,运行于后台,耗时的操作可以在这里做
  • publishProgress()更新进度,给onProgressUpdate()传递进度参数
  • onProgressUpdate()publishProgress()调用完被调用,更新进度
  • onPostExecute(Result) :doInBackGround()方法执行后的返回结果会传给该方法,此方法运行在UI线程里(主线程),在该方法中可以处理显示结果(eg:给TextView 控件设置内容、ListView控件设置适配器……)
  • onPreExecute():该方法依旧运行在主线程,当AsyncTask的execute()方法执行后,onPreExecute()会首先执行。可以在该方法中做准备工作,eg:初始化进度条。
好了,看下实际的例子,了解一下怎么使用吧:
[java] view plaincopy
  1. public class MyActivity extends Activity  
  2. {  
  3.     private Button btn;  
  4.     private TextView tv;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         btn = (Button) findViewById(R.id.start_btn);  
  11.         tv = (TextView) findViewById(R.id.content);  
  12.         btn.setOnClickListener(new Button.OnClickListener(){  
  13.             public void onClick(View v) {  
  14.                 update();  
  15.             }  
  16.         });  
  17.     }  
  18.     private void update(){  
  19.         UpdateTextTask updateTextTask = new UpdateTextTask(this);  
  20.         updateTextTask.execute();  
  21.     }  
  22.   
  23.     class UpdateTextTask extends AsyncTask<Void,Integer,Integer>{  
  24.         private Context context;  
  25.         UpdateTextTask(Context context) {  
  26.             this.context = context;  
  27.         }  
  28.   
  29.         /** 
  30.          * 运行在UI线程中,在调用doInBackground()之前执行 
  31.          */  
  32.         @Override  
  33.         protected void onPreExecute() {  
  34.             Toast.makeText(context,"开始执行",Toast.LENGTH_SHORT).show();  
  35.         }  
  36.         /** 
  37.          * 后台运行的方法,可以运行非UI线程,可以执行耗时的方法 
  38.          */  
  39.         @Override  
  40.         protected Integer doInBackground(Void... params) {  
  41.             int i=0;  
  42.             while(i<10){  
  43.                 i++;  
  44.                 publishProgress(i);  
  45.                 try {  
  46.                     Thread.sleep(1000);  
  47.                 } catch (InterruptedException e) {  
  48.                 }  
  49.             }  
  50.             return null;  
  51.         }  
  52.   
  53.         /** 
  54.          * 运行在ui线程中,在doInBackground()执行完毕后执行 
  55.          */  
  56.         @Override  
  57.         protected void onPostExecute(Integer integer) {  
  58.             Toast.makeText(context,"执行完毕",Toast.LENGTH_SHORT).show();  
  59.         }  
  60.   
  61.         /** 
  62.          * 在publishProgress()被调用以后执行,publishProgress()用于更新进度 
  63.          */  
  64.         @Override  
  65.         protected void onProgressUpdate(Integer... values) {  
  66.             tv.setText(""+values[0]);  
  67.         }  
  68.     }  
  69. }