1.什么是AsyncTask
AsyncTask是安卓提供的轻量级(实现代码少)的异步类。
为了减低异步通信的开发难度,提供AsyncTask。
AsyncTask直接继承与object类,位于androi.os包中。
使用AsyncTask可以忽略looper、MessageQueue、Handler等复杂对象,更省时。
2.如何使用AsyncTask
新建内部类继承AsyncTask。
定义AsyncTask三种泛型参数,重写doInBackground抽象方法,重写onPostExecute、onProgressUpdate方法。
在需要启动的地方调用execute方法。
3.使用AsyncTask做倒计时
首先将按键、布局设定好;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ll.handler.TaskActivity">
<EditText
android:id="@+id/ed"
android:layout_width="match_parent"
android:layout_height="50dp" />
<TextView
android:gravity="center"
android:text="倒计时"
android:textSize="30sp"
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="200dp" />
<Button
android:text="开始计时"
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
然后绑定Id
private void BIndId() {
editText=findViewById(R.id.ed);
textView=findViewById(R.id.tv);
button=findViewById(R.id.btn);
}
接着创建内部类继承syncTask,重写doInBackground抽象方法,重写onPostExecute、onProgressUpdate方法。
class myabc extends AsyncTask<Integer,Integer,String> {
@Override
protected String doInBackground(Integer... integers) {
for (int i=0;i<integers[0];i++){
try {
Thread.sleep(1000);
publishProgress(i);//调用onProgressUpdate方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "计时结束";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
textView.setText("计时:"+values[0]);
}
@Override
//耗时操作完成时,请调用此方法(主线程)
protected void onPostExecute(String s) {
super.onPostExecute(s);
textView.setText(s);
}
}
}
最后 在需要启动的地方调用execute方法。
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int time=Integer.parseInt(editText.getText().toString());
new myabc().execute(time);
}
});
4.使用AsyncTask做进度条
其实进度条与做倒计时差不多。
首先将按键、布局设定好;
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ll.handler.Async">
<ProgressBar
android:id="@+id/pbar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="300dp" />
<Button
android:id="@+id/kaishibtn"
android:text="开始下载"
android:layout_width="match_parent"
android:layout_height="50dp" />
<Button
android:id="@+id/quxiao"
android:text="取消下载"
android:layout_width="match_parent"
android:layout_height="50dp" />
</LinearLayout>
rivate void BindId() {
zbtv=findViewById(R.id.zbxztv);
ksbtn=findViewById(R.id.zsxabtn);
}
然后绑定Id
private void BindId() {
bar=findViewById(R.id.pbar);
btn=findViewById(R.id.kaishibtn);
btn.setOnClickListener(this);
bar.setMax(10);
}
接着创建内部类继承syncTask,重写doInBackground抽象方法,重写onPostExecute、onProgressUpdate方法。
lass mycvb extends AsyncTask<Integer,Integer,Integer>{
@Override
protected Integer doInBackground(Integer... integers) {
for(int i=1;i<=10;i++){
try {
Thread.sleep(1000);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return 1;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
bar.setProgress(values[0]);
}
@Override
protected void onPostExecute(Integer s) {
super.onPostExecute(s);
btn.setText("下载完成");
}
}
}
最后 在需要启动的地方调用execute方法。
public void onClick(View view) {
switch (view.getId()){
case R.id.btn:
btn.setEnabled(false);
btn.setText("正在下载");
new mycvb().execute();
break;
}
}
5.Execute()和executeOnExecutor有何区别
Execute方法:
启动异步消息,AsyncTask中的方法会被系统统一调用
Execute方法必须在UI线程中调用,使用Execute启动多个任务时,会按照现后顺序逐一进行。
executeOnExecutor:
启动异步消息,AsyncTask中的方法会被系统统一调用
executeOnExecutor方法必须在UI线程中调用,使用executeOnExecutor可以同时启动多个任务时,最多5个一起执行。
6.更喜欢AsyncTask还是Handler
其实吧,我觉得谈不上那个更喜欢,但是只有一点可以肯定时那个方便用哪个。