·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> Android开发 >> Android中AsyncTask异步任务使用详细实例(一)

Android中AsyncTask异步任务使用详细实例(一)

作者:佚名      Android开发编辑:admin      更新时间:2022-07-23

AsyncTask是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主线程。

使用AsyncTask最少要重写以下两个方法:

1、doInBackground(Params…) 后台执行,比较耗时的操作都可以放在这里。注意这里不能直接操作UI。此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间。在执行过程中可以调用publicProgress(Progress…)来更新任务的进度。

2、onPostExecute(Result) 在这里面可以使用在doInBackground 得到的结果处理操作UI。 此方法在主线程执行,任务执行的结果作为此方法的参数返回 。

MainActivity如下:

package com.example.asynctasktest;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button satrtButton;
private Button cancelButton;
private ProgressBar progressBar;
private TextView textView;
private DownLoaderAsyncTask downLoaderAsyncTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView();
}
public void initView() {
satrtButton=(Button) findViewById(R.id.startButton);
cancelButton=(Button) findViewById(R.id.cancelButton);
satrtButton.setOnClickListener(new ButtonOnClickListener());
cancelButton.setOnClickListener(new ButtonOnClickListener());
progressBar=(ProgressBar) findViewById(R.id.progressBar);
textView=(TextView) findViewById(R.id.textView);
}
private class ButtonOnClickListener implements OnClickListener{
public void onClick(View v) {
switch (v.getId()) {
case R.id.startButton:
//注意:
//1 每次需new一个实例,新建的任务只能执行一次,否则会出现异常
//2 异步任务的实例必须在UI线程中创建
//3 execute()方法必须在UI线程中调用。
downLoaderAsyncTask=new DownLoaderAsyncTask();
downLoaderAsyncTask.execute("http://www.baidu.com");
break;
case R.id.cancelButton:
//取消一个正在执行的任务,onCancelled()方法将会被调用 
downLoaderAsyncTask.cancel(true);
break;
default:
break;
}
}
}
//构造函数AsyncTask<Params, Progress, Result>参数说明: 
//Params 启动任务执行的输入参数
//Progress 后台任务执行的进度
//Result 后台计算结果的类型
private class DownLoaderAsyncTask extends AsyncTask<String, Integer, String>{
//onPreExecute()方法用于在执行异步任务前,主线程做一些准备工作 
@Override
protected void onPreExecute() {
super.onPreExecute();
textView.setText("调用onPreExecute()方法--->准备开始执行异步任务");
System.out.println("调用onPreExecute()方法--->准备开始执行异步任务");
}
//doInBackground()方法用于在执行异步任务,不可以更改主线程中UI 
@Override
protected String doInBackground(String... params) {
System.out.println("调用doInBackground()方法--->开始执行异步任务");
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
long total = entity.getContentLength();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count = 0;
int length = -1;
while ((length = is.read(buffer)) != -1) {
bos.write(buffer, 0, length);
count += length;
//publishProgress()为AsyncTask类中的方法
//常在doInBackground()中调用此方法
//用于通知主线程,后台任务的执行情况.
//此时会触发AsyncTask中的onProgressUpdate()方法
publishProgress((int) ((count / (float) total) * 100));
//为了演示进度,休眠1000毫秒
Thread.sleep(1000);
}
return new String(bos.toByteArray(), "UTF-8");
}
} catch (Exception e) {
return null;
}
return null;
}
//onPostExecute()方法用于异步任务执行完成后,在主线程中执行的操作
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result); 
Toast.makeText(getApplicationContext(), "调用onPostExecute()方法--->异步任务执行完毕", 0).show();
//textView显示网络请求结果
textView.setText(result);
System.out.println("调用onPostExecute()方法--->异步任务执行完毕");
}
//onProgressUpdate()方法用于更新异步执行中,在主线程中处理异步任务的执行信息 
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
//更改进度条
progressBar.setProgress(values[0]);
//更改TextView
textView.setText("已经加载"+values[0]+"%");
}
//onCancelled()方法用于异步任务被取消时,在主线程中执行相关的操作 
@Override
protected void onCancelled() {
super.onCancelled();
//更改进度条进度为0
progressBar.setProgress(0);
//更改TextView
textView.setText("调用onCancelled()方法--->异步任务被取消");
System.out.println("调用onCancelled()方法--->异步任务被取消");
}
}
}

main.xml如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/startButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始异步任务" />
<Button
android:id="@+id/cancelButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="取消异步任务" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test test" />
</ScrollView>
</LinearLayout>

以上内容是小编给大家介绍的Android中AsyncTask异步任务使用详细实例(一),希望对大家有所帮助!