介绍:
Android是单线程耗时操作要在非主线程中进行
AsyncTask可以在子线程中跟新UI、封装、简化异步操作
使用:
1、创建一个类继承AsyncTask
public class Myaya extends AsyncTask{}
2、了解参数和@Override他的三个方法
AsyncTask<Params, Progress, Result>
是一个抽象类,通常用于被继承,继承AsyncTask需要指定如下三个泛型参数:Params:启动任务时输入参数的类型、Progress:后台任务执行中返回进度值的类型、Result:后台执行任务完成后返回结果的类型
注意:参数不是一定要使用, private class MyTask extends AsyncTask<Void, Void, Void> { ... }
3. 在继承AsyncTask的子类中需要重写的回调方法
(如图)new DatashujuAsyncTask().execute();
1、 onPreExecute()//onPreExecute方法用于在执行后台任务前做一些UI操作
2、doInBackground(Params..)//doInBackground方法内部执行后台任务,不可在此方法内修改UI
3、>onPostExecute(Result)//onPostExecute方法用于在执行完后台任务后更新UI,显示结果
4、onProgressUpdate(Integer... progresses)//onProgressUpdate方法用于更新进度信息
Android是单线程耗时操作要在非主线程中进行
AsyncTask可以在子线程中跟新UI、封装、简化异步操作
使用:
1、创建一个类继承AsyncTask
public class Myaya extends AsyncTask{}
2、了解参数和@Override他的三个方法
AsyncTask<Params, Progress, Result>
是一个抽象类,通常用于被继承,继承AsyncTask需要指定如下三个泛型参数:Params:启动任务时输入参数的类型、Progress:后台任务执行中返回进度值的类型、Result:后台执行任务完成后返回结果的类型
注意:参数不是一定要使用, private class MyTask extends AsyncTask<Void, Void, Void> { ... }
3. 在继承AsyncTask的子类中需要重写的回调方法
(如图)new DatashujuAsyncTask().execute();
1、 onPreExecute()//onPreExecute方法用于在执行后台任务前做一些UI操作
2、doInBackground(Params..)//doInBackground方法内部执行后台任务,不可在此方法内修改UI
3、>onPostExecute(Result)//onPostExecute方法用于在执行完后台任务后更新UI,显示结果
4、onProgressUpdate(Integer... progresses)//onProgressUpdate方法用于更新进度信息
Activity需要的代码
需要注意的是AsyncTask执行没有结束时不能.execute();
package com.zxx.asynctaskupdata;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
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 {
ProgressBar bar;
MyAsyncTaskUpdata mtask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mtask=new MyAsyncTaskUpdata();
bar=(ProgressBar) findViewById(R.id.progressBar1);
Button button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//一个任务没有执行完之前不能执行宁一个任务
mtask.execute();
Log.i("*****", "mtask启动"+mtask);
}
});
}
/**
* 手机返回时会执行onpause()
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//判断AsyncTask不为null且Status.RUNNING在运行状态
if (mtask!=null&&
mtask.getStatus()==Status.RUNNING) {
//为mtask标记cancel(true)状态
mtask.cancel(true);
}
}
public class MyAsyncTaskUpdata extends AsyncTask<Void, Integer, Void>{
@Override
protected Void doInBackground(Void... arg0) {
for (int i = 0; i < 100; i++) {
//判断mtask标记cancel(true)状态结束整个任务
if (isCancelled()) {
break;
}
publishProgress(i);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.i("*****", "doInBackground启动");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
protected void onProgressUpdate(Integer... integers) {
super.onProgressUpdate(integers);
//mtask标记cancel(true)不做更新处理
if (isCancelled()) {
return;
}
//这里传进来的更新值 是一个数组队列我们只取 第1个
bar.setProgress(integers[0]);
}
}
}
xml文件的属性
<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="com.zxx.asynctaskupdata.MainActivity" >
<ProgressBar
android:id="@+id/progressBar1"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_marginBottom="76dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="190dp"
android:text="点击启动更新" />
</RelativeLayout>
AsyncTask注意事项:
1、必须在UI线程中创建AsyncTask实例
2、必须在UI线程中调用AsyncTask的execute()方法
3、重写的四个方法使系统自动调用,不应手动调用
4、每个AsyncTask只能执行一次。多次会运行错误