进程和线程,打算在后面一节会细说下,这里先简单那的介绍他们在android中是怎么回事。
在android,当一个应用启动的时候,linux内核会启动一个进程和一个单线程(就是我们所说的主线程),主线程处理与UI相关的事情,如: 绘图事件,触摸,点击事件等;但是有些时候我们需要做一些操作会阻塞主线程,如:网络下载,文件的读取,写入等,这样造成用户体验不好,容易产生ANR的错误。所以一般对与比较耗时的操作,一般会另外开一个线程,在这个线程里进行才操作;
而google为了方便开发者处理异步任务,就给出了异步任务的大杀器——AsyncTask,他确实是另外创建了一个新的线程,这点和handler不同,这会在以后的handler中细说下。这样作为开发者,就不需要处理那些handler,thread。
一句话:AsyncTask就是android为了降低开发难度,减少复杂度,而在handler的基础上而诞生的东西;也就是封装在后台的任务类,说白了就是异步任务。
AsyncTask的用法:
1.生成一个子类;
2.实现2个必须实现和两个选择性实现的方法(2+2)
onPreExecute(), 该方法将在执行后台操作前被UI thread调用。可以在该方法中做一些准备工作,如在界面上显示一个进度条。
doInBackground(Params...), 将在onPreExecute 方法执行后马上执行,该方法运行在后台线程中。这里将主要负责执行那些很耗时的后台计算工作。可以调用 publishProgress方法来更新实时的任务进度。
onProgressUpdate(Progress...),在publishProgress方法被调用后,UI thread将调用这个方法从而在界面上展示任务的进展情况,例如通过一个进度条进行展示。
onPostExecute(Result), 在doInBackground 执行完成后,onPostExecute 方法将被UI thread调用,后台的计算结果将通过该方法传递到UI thread.
其中,doInBackground(Params...), onPostExecute(Result)是必须是实现的。
AsyncTask的三个泛型参数,Params,Progress,Result
1.Params, 传给AsyncTask需要执行的参数;
2.Progress, 计算后台任务处理的进度;
3.Result, 在后台任务处理好后,把结果返回,如网页,返回网页的内容;
AsyncTask调用的4大准则准则:
a) Task的实例必须在UI thread中创建;
b) execute方法必须在UI thread中调用;
c) 不要手动的调用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)这几个方法;
d) 该task只能被执行一次,否则多次调用时将会出现异常
doInBackground方法和onPostExecute的参数必须对应,这两个参数在AsyncTask声明的泛型参数列表中指定,第一个为doInBackground接受的参数,第二个为显示进度的参数,第三个为doInBackground返回和onPostExecute传入的参数。
介绍从网上下载一个网页,并显示网页的内容的例子:
class DownLoadTask extends AsyncTask<String, Integer, String> {
ProgressDialog progressdialog;
public DownLoadTask(Context context){
progressdialog = new ProgressDialog(context, 0);
progressdialog.setButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
progressdialog.cancel();
}
});
pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
finish();
}
});
progressdialog.setCancelable(true);
progressdialog.setMax(100);
progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
@Override
protected String doInBackground(String... params) {
try{
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
// 获取网页中内容的字节数
long totalLength = entity.getContentLength();
InputStream is = entity.getContent();
String s = null;
if(is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count += ch;
if(length > 0) {
// 如果知道响应的长度,调用publishProgress()更新进度
publishProgress((int) ((count / (float) totalLength) * 100));
}
// 让线程休眠100ms
Thread.sleep(100);
}
s = new String(baos.toByteArray());
}
// 返回结果
return s;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
// 在doInBackground完成后,会收到处理好的结果result
progressdialog.dismiss();
}
@Override
protected void onPreExecute() {
// 执行了execute
progressdialog.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// 更新进度
progressdialog.setProgress(values[0]);
}
}
以上就是自定义的DownLoadTask,主要在doInBackground中进行了下载处理;
至于下载的方法即相关的只是,偶会在后面的章节中给大家解释的,如果有需要的,可以留言。如果对于I/O流的操作不是很熟悉,那就看下java的相关知识。
在UI线程中启动它,
DownLoadTask task = new PageTask(this);
task.execute("http://www.baidu.com");
注意:
AsyncTask是多线程,因为他有新创建一个线程,并且还有线程池;
所以他的资源的消耗要比handler要多;当然敲得代码量就比handler少了。