之前,在学习多线程的时候,我首先学习了Handler的用法,了解了安卓中的通过使用Handler、Message、Looper、Thread来实现多线程操作的方法。
其实在安卓(Android 1.5)中提供了一个叫做AsyncTask的工具类,它使创建需要与用户界面交互的长时间运行的任务变得更简单。从今天开始,我将学习AsyncTask的一些用法和原理。
首先看一下,在API中对AsyncTask的说明:
AsyncTaskenables proper and easy use of the UI thread. This class allows to performbackground operations and publish results on the UI thread without having tomanipulate threads and/or handlers.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute ageneric threading framework. AsyncTasks should ideally be used for shortoperations (a few seconds at the most.) If you need to keep threads running forlong periods of time, it is highly recommended you use the various APIsprovided by thejava.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.
我简单翻译一下:AsyncTask提供了对UI线程简单应用的操作。这个类允许在不使用多线程和Handler的情况下,在后台执行操作并将结果反馈给UI线程。
AsyncTask被设计为Thread和Handler的帮助类,它没有构造通用的线程的架构(这句话不太理解,翻译的不好)。对AsyncTasks合理的使用方法是用来进行短时间的操作(最多几秒钟)。如果你需要让线程持续运行较长时间的话,强烈建议你去使用java.util.concurrent包中提供的其它API,比如Executor, ThreadPoolExecutor 和 FutureTask。
通过API中说明,我们对AsyncTask的作用和使用场景有了最基本的认识,但是,想要深入了解AsyncTask还是让我们来看代码吧
下面代码实现的功能和我在学习Handler时是一样的
点击启动按钮,界面中出现一个进度条,并以每秒10%的进度增加,当增加到100%的时候,进度条消失。在这个过程中,我们也可以通过点击取消按钮来取消这个操作
下面是代码
package com.example.asynctask1; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends Activity { Button startButton = null; Button stopButton = null; ProgressBar progressbar = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startButton = (Button) findViewById(R.id.startButton); stopButton = (Button) findViewById(R.id.stopButton); progressbar = (ProgressBar) findViewById(R.id.progressBar); //为button绑定onclicklistener startButton.setOnClickListener(new ButtonOnclickListener()); stopButton.setOnClickListener(new ButtonOnclickListener()); } class ButtonOnclickListener implements OnClickListener{ public void onClick(View v) { ProgressTask progressTask = new ProgressTask(); switch(v.getId()){ case R.id.startButton: //启动线程 progressTask.execute(1); progressbar.setVisibility(View.VISIBLE); break; case R.id.stopButton: //线程取消 progressTask.cancel(true); progressbar.setVisibility(View.GONE); break; } } } class ProgressTask extends AsyncTask<Integer, Integer, String>{ protected String doInBackground(Integer... params) { int i = params[0]; while(i<100){ try { //控制线程延迟1秒 Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i += 10; //将更新内容发布到UI线程中,该方法会触发onProgressUpdate publishProgress(i); } return null; } //onProgressUpdate提供了可以更新UI的场所 protected void onProgressUpdate(Integer... values) { Log.i("run", "run "+values[0]+"%"); progressbar.setProgress(values[0]); if(values[0]>100) progressbar.setVisibility(View.GONE); } } }
在上面代码中我们可以看到,要使用AsyncTask,我们首先要有一个继承AsyncTask的子类,在子类中重载一些方法。
下面是今天学习的重点,AsyncTask的三个泛型类型和四个步骤
三个泛型类型:
class ProgressTask extends AsyncTask<Integer,Integer, String>
AsyncTask定义了三种泛型类型Params,Progress和Result。
四个步骤:
我在代码中重载了doInBackground、onProgressUpdate两个方法,其实在异步任务执行的时候,有四个步骤,对应四个方法:
这四个方法不需要我们手动去调用,当异步任务执行到相应步骤时,会自动调用这几个方法,如果有需要,我们可以在声明类的时候重载其中的方法。
android多线程之AsyncTask的初步认识(一),布布扣,bubuko.com
原文:http://blog.csdn.net/xiapinnong/article/details/22741673