安卓 AsyncTask 概述
安卓 AsyncTask是 Android 提供的一个抽象类,它允许我们在后台执行繁重的任务,并且保持 UI 线程轻量化,从而使应用程序响应速度更快。
应用程序启动时运行在一个线程上。由于这个单线程模型的任务需要更长的时间来获取响应,因此会导致应用程序无响应。为了避免这种情况,我们使用 android AsyncTask 在专用线程上执行后台繁重的任务,并将结果传递回 UI 线程。因此,在 android 应用程序中使用 AsyncTask 可以使 UI 线程始终保持响应。
在 android AsyncTask 类中使用的基本方法定义如下:
- doInBackground() : 此方法包含需要在后台执行的代码。在此方法中,我们可以通过 publishProgress ()方法多次向 UI 线程发送结果。要通知后台处理已经完成,我们只需要使用 return 语句
- onPreExecute() : 此方法包含在后台处理开始之前执行的代码
- onPostExecute() :在 doInBackground 方法完成处理后调用此方法
- onProgressUpdate() : 此方法从 doInBackground 方法接收进度更新,该方法通过 publishProgress 方法发布,此方法可以使用此进度更新更新更新 UI 线程
在 android AsyncTask 类中使用的三个泛型类型如下:
- Params : 在执行时发送给任务的参数类型
- Progress : 在后台计算期间发布的进度单元的类型
- Result : 背景计算结果的类型
AsyncTask 示例
要启动 AsyncTask,下面的代码片段必须出现在 MainActivity 类中:
MyTask myTask = new MyTask();
myTask.execute();
在上面的代码片段中,我们使用了一个扩展 AsyncTask 和 execute 方法的样例类名来启动后台线程。
注意:
- 必须在 UI 线程中创建并调用 AsyncTask 实例
- 不应该调用 AsyncTask 类中重写的方法,它们是自动调用的
- 只能调用一次。再次执行将抛出异常
在本教程中,我们将实现一个 AsyncTask,它使进程在用户设置的给定时间段内进入睡眠状态。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10pt"
android:textColor="#444444"
android:layout_alignParentLeft="true"
android:layout_marginRight="9dip"
android:layout_marginTop="20dip"
android:layout_marginLeft="10dip"
android:text="Sleep time in Seconds:"/>
<EditText
android:id="@+id/in_time"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_toRightOf="@id/tv_time"
android:layout_alignTop="@id/tv_time"
android:inputType="number"
/>
<Button
android:id="@+id/btn_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Run Async task"
android:layout_below="@+id/in_time"
android:layout_centerHorizontal="true"
android:layout_marginTop="64dp" />
<TextView
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="7pt"
android:layout_below="@+id/btn_run"
android:layout_centerHorizontal="true" />
</RelativeLayout>
package com.example.myapplication2;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText time;
private TextView finalResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (EditText) findViewById(R.id.in_time);
button = (Button) findViewById(R.id.btn_run);
finalResult = (TextView) findViewById(R.id.tv_result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTaskRunner runner = new AsyncTaskRunner();
String sleepTime = time.getText().toString();
runner.execute(sleepTime);
}
});
}
private class AsyncTaskRunner extends AsyncTask<String, String, String> {
private String resp;
ProgressDialog progressDialog;
@Override
protected String doInBackground(String... params) {
publishProgress("Sleeping..."); // Calls onProgressUpdate()
try {
int time = Integer.parseInt(params[0])*1000;
Thread.sleep(time);
resp = "Slept for " + params[0] + " seconds";
} catch (InterruptedException e) {
e.printStackTrace();
resp = e.getMessage();
} catch (Exception e) {
e.printStackTrace();
resp = e.getMessage();
}
return resp;
}
@Override
protected void onPostExecute(String result) {
// execution of result of Long time consuming operation
progressDialog.dismiss();
finalResult.setText(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(MainActivity.this,
"ProgressDialog",
"Wait for "+time.getText().toString()+ " seconds");
}
@Override
protected void onProgressUpdate(String... text) {
finalResult.setText(text[0]);
}
}
}