AsyncTask处理是安卓中处理多个线程之间通讯的最主要的方法。
|
No. |
方法 |
类型 |
描述 |
|
1 |
public final boolean cancel(booleanmayInterruptIfRunning) |
普通 |
指定是否取消当前线程操作 |
|
2 |
public final AsyncTask<Params, Progress, Result> execute(Params... params) |
普通 |
执行AsyncTask操作 |
|
3 |
public final booleanisCancelled() |
普通 |
判断子线程是否被取消 |
|
4 |
protected final void publishProgress(Progress... values) |
普通 |
更新线程进度 |
|
5 |
protected abstract Result doInBackground(Params... params) |
普通 |
在后台完成任务执行,可以调用publishProgress()方法更新线程进度 |
|
6 |
protected void onProgressUpdate(Progress... values) |
普通 |
在主线程中执行,用于显示任务的进度 |
|
7 |
protected void onPreExecute() |
普通 |
在主线程中执行,在doInBackground()之前执行 |
|
8 |
protected void onPostExecute(Result result) |
普通 |
在主线程中执行,方法参数为任务执行结果 |
|
9 |
protected void onCancelled() |
普通 |
主线程中执行,在cancel()方法之后执行 |
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar1"
android:layout_alignParentTop="true"
android:layout_marginTop="162dp"
android:text="" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="54dp" />
</RelativeLayout>
主程序如下:
package com.example.ASyncTask;
import android.os.AsyncTask;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.Menu;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView info = null;
private ProgressBar mybar = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.info = (TextView) super.findViewById(R.id.textView1);
this.mybar = (ProgressBar) super.findViewById(R.id.progressBar1);
new ChildUpdate().execute(100);//创建匿名对象执行程序
}
private class ChildUpdate extends AsyncTask<Integer, Integer, String> {
@Override
protected String doInBackground(Integer... params) {// 后台处理的程序
for (int i = 0; i < 100; i++) {
MainActivity.this.mybar.setProgress(i);// 执行传入操作
this.publishProgress(i);// 更新每次的那内容,把值传给onProgressupdate()进行处理
try {
Thread.sleep(params[0]);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "执行完毕";
}
@Override
protected void onPostExecute(String result) {// 执行完毕后的操作
MainActivity.this.info.setText(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
MainActivity.this.info.setText("当前进度为:"
+ String.valueOf(values[0]));
}
}
}
安卓异步处理的使用AsyncTask,布布扣,bubuko.com
原文:http://blog.csdn.net/u013616976/article/details/21390303