Android异步任务AsyncTask实现方式

来源:互联网 发布:java打印 编辑:程序博客网 时间:2024/05/17 08:36

在Android编程中,异步操作是必须掌握的基本知识。在Android中UI线程响应不能超过5s,否则会出现ANR,因此我们常常将耗时操作放在非工作线程中执行。AsyncTask是Android推荐实现异步操作的方式,希望大家熟练掌握。

首先,我们实现异步操作需要继承AsyncTask类,并定义三个返回类型(没有返回类型使用void),具体代码如下所示:

[java] view plaincopyprint?
  1. class UpdateTextTask extends AsyncTask<Void, Integer, Integer> 

通过打印回调方法的Log日志,三个参数类型依次代表:

第一个Void参数类型代表doInBackground(Void... params)回调方法的参数类型

第二个Integer 参数类型代表onProgressUpdate(Integer... values)的参数类型,该方法在UI线程中执行。

第三个Integer参数类型代表doInBackground的返回值和onPostExecute(Integer integer)(该方法在UI线程中执行)方法的参数类型

实例代码如下:

[java] view plaincopyprint?
  1. import android.app.Activity; 
  2. import android.content.Context; 
  3. import android.os.AsyncTask; 
  4. import android.os.Bundle; 
  5. import android.view.View; 
  6. import android.widget.Button; 
  7. import android.widget.TextView; 
  8. import android.widget.Toast; 
  9.  
  10. public class MyActivityextends Activity { 
  11.     private Button btn; 
  12.     private TextView tv; 
  13.  
  14.     @Override 
  15.     public void onCreate(Bundle savedInstanceState) { 
  16.         super.onCreate(savedInstanceState); 
  17.         setContentView(R.layout.main); 
  18.         btn = (Button) findViewById(R.id.start_btn); 
  19.         tv = (TextView) findViewById(R.id.content); 
  20.         btn.setOnClickListener(new Button.OnClickListener() { 
  21.             public void onClick(View v) { 
  22.                 update(); 
  23.             } 
  24.         }); 
  25.     } 
  26.  
  27.     private void update() { 
  28.         UpdateTextTask updateTextTask = new UpdateTextTask(this); 
  29.         updateTextTask.execute(); 
  30.     } 
  31.  
  32.     class UpdateTextTask extends AsyncTask<Void, Integer, Integer> { 
  33.         private Context context; 
  34.  
  35.         UpdateTextTask(Context context) { 
  36.             this.context = context; 
  37.         } 
  38.  
  39.         /**
  40.          * 运行在UI线程中,在调用doInBackground()之前执行
  41.          */ 
  42.         @Override 
  43.         protected void onPreExecute() { 
  44.             Toast.makeText(context, "开始执行", Toast.LENGTH_SHORT).show(); 
  45.         } 
  46.  
  47.         /**
  48.          * 后台运行的方法,可以运行非UI线程,可以执行耗时的方法
  49.          */ 
  50.         @Override 
  51.         protected Integer doInBackground(Void... params) { 
  52.             int i = 0
  53.             while (i < 10) { 
  54.                 i++; 
  55.                 publishProgress(i); 
  56.                 try
  57.                     Thread.sleep(1000); 
  58.                 } catch (InterruptedException e) { 
  59.                 } 
  60.             } 
  61.             return null
  62.         } 
  63.  
  64.         /**
  65.          * 运行在ui线程中,在doInBackground()执行完毕后执行
  66.          */ 
  67.         @Override 
  68.         protected void onPostExecute(Integer integer) { 
  69.             Toast.makeText(context, "执行完毕", Toast.LENGTH_SHORT).show(); 
  70.         } 
  71.  
  72.         /**
  73.          * 在publishProgress()被调用以后执行,publishProgress()用于更新进度
  74.          */ 
  75.         @Override 
  76.         protected void onProgressUpdate(Integer... values) { 
  77.             System.out.println("do onProgressUpdate........."); 
  78.             tv.setText("" + values[0]); 
  79.         } 
  80.     } 

AsyncTask异步任务的时序图如图1-1所示:



                          图1-1AsyncTask异步加载时序图

说明:AsyncTask对象执行excute()方法后,内部方法执行的顺序:

  1. 执行onPreExecute(),该方法在UI线程中执行;
  2. 执行doInBackground(),该方法并不在UI线程中,因此不能对UI控件进行设置和修改;
  3. 执行publishProgress(i),每次调用都会触发onProgressUpdate()方法,该方法的参数值会赋值给onProgressUpdate的参数;
  4. 在doInBackground()执行完成后,调用onpostExecute()方法。 doInBackground()的返回值,传递给onpostExecute()方法。
0 0
原创粉丝点击