AsyncTask
1.什么是AsyncTask?
- Async Task是 Android提供的轻量级(实现代码量少)的异步类。
- 为了降低异步通信的开发难度,提供了 Asynctask。
- Asynctask直接继承于object类,位于 android os包中。
使用 Asynctask可以忽略Looper、MessageQueue、Hander等复杂对象,便捷的完成异步耗时操作。
AsyncTask的执行过程:
U线程→启动 Async Task→执行耗时操作→接收消息
2.如何使用AsyncTask
使用 AsyncTask步骤:
1)新建内部类继承 Async Task;
2)定义 Asynctask的三种泛型参数
3)重写doluBackGround抽象方法;
4)重写onPreExcute方法;
5)重写onProgressUpdate方法;
6)重写onPostExecute方法;
7)在需要启动的地方调用execute方法。
3.使用 Asynctask做倒计时(附代码)
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.texthandler.Main4Activity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置时间"
android:textSize="30sp"/>
<EditText
android:id="@+id/szsj4"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_toRightOf="@+id/text1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="秒"
android:layout_toRightOf="@+id/szsj4"
android:textSize="30sp"/>
<TextView
android:id="@+id/djs_text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="倒计时"
android:textSize="30dp"/>
<Button
android:id="@+id/djs_btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始倒计时"
android:layout_below="@+id/djs_text4"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
java代码:
package com.example.texthandler;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main4Activity extends AppCompatActivity {
private TextView djstext4;
private Button djsbtn4;
private EditText djset4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
bindID();
djsbtn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new MyTask().execute();
}
});
}
private void bindID() {
djstext4=findViewById(R.id.djs_text);
djsbtn4=findViewById(R.id.djs_btn);
djset4=findViewById(R.id.szsj);
}
class MyTask extends AsyncTask<Integer,Integer,String>{
@Override
protected String doInBackground(Integer... integers) {
for (int i=integers[0];i>0 ;i--){
try{
Thread.sleep(1000);
publishProgress(i);//调用onProgressUpdate方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "计时结束";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
djstext4.setText(values[0]+"");
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
djstext4.setText(s);
}
}
}
4.使用 Asynctask做进度条(附代码)
xml代码:
<TextView
android:id="@+id/download_text"
android:layout_width="match_parent"
android:layout_height="60dp"
android:textSize="30sp"
android:text="开始下载"/>
<Button
android:id="@+id/doswnload_btn"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="开始下载"
android:textSize="30sp"/>
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="60dp" />
java代码:
public class ATActivity extends AppCompatActivity {
private Button downloadBtn;
private TextView downloadText;
private ProgressBar downloadBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_at);
bindID();
downloadBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Mytask mt = new Mytask();
mt.execute(1000);
}
});
}
private void bindID() {
downloadBtn = findViewById(R.id.doswnload_btn);
downloadText = findViewById(R.id.download_text);
downloadBar=findViewById(R.id.progress_bar);
}
public class Mytask extends AsyncTask<Integer, Integer, String> {
//耗时操作执行前,调用此方法
@Override
protected void onPreExecute() {
super.onPreExecute();
downloadText.setText("正在下载");
}
//执行耗时操作
@Override
protected String doInBackground(Integer... Integer) {//...代表参数个数是不确定的
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(Integer[0]);
publishProgress(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "下载完成";
}
//耗时操作完成后,调用此方法
@Override
protected void onPostExecute(String s) {
downloadText.setText(s);
super.onPostExecute(s);
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
downloadBar.setProgress(values[0]);
}
}
}
5.Execute()和 executeonexecutor有何区别?
1)执行execute()方法线程可以只能串行运行。
2)执行executeOnExecuter()方法线程并行运行,不过同一时间只能启动五个线程。
6.更喜欢 Asynctask还是更喜欢 Handler,为什么?
更喜欢Hanadler,Handle结构清晰,功能定义明确,对于多个后台任务时,简单,清晰。