本节概述了android-asynctask是什么,以及开发人员为什么要使用它。
它还应该提到android-asynctask中的任何大型主题,并链接到相关主题。由于android-asynctask的文档是新的,您可能需要创建这些相关主题的初始版本。
概念
AsyncTask是一个允许在后台运行操作的类,其结果在UI线程上发布。主要目的是通过消除处理程序和操作线程所需的所有东西来消除启动/运行线程的所有样板代码。此外,AsyncTask的目的是在后台线程上进行短时操作(最多几秒),而不是长时间操作。因此,重要的是AsyncTask不要与通用线程框架混淆。如果需要进行长时间操作,则建议使用并发包。
一般考虑
AsyncTask由三种泛型类型定义:参数,进度和结果。从执行的那一刻起,它经历了4个步骤(方法)。首先是onPreExecute ,有人可以定义一个加载对话框,或者一些可以通知用户执行即将开始的UI消息。接下来, doInBackground是在与Ui线程不同的线程上异步运行的方法。第三种方法是onProgressUpdate ,它也可以在UI线程上运行,该线程可以通知用户状态。调用它的最后一个方法是onPostExecute,它主要用于发布结果。
下面是一个如何使用AsyncTask返回字符串的示例。
例1
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (FloatingActionButton) findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
executeAsyncTaskOperation();
}
});
}
private void executeAsyncTaskOperation() {
new CustomAsyncTask(this).execute();
}
private static class CustomAsyncTask extends AsyncTask<Void, Void, String> {
private Context context;
private ProgressDialog progressDialog;
public CustomAsyncTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Please wait...", "Loading data from web");
}
@Override
protected String doInBackground(Void... params) {
String object = null;
try {
Log.d(CustomAsyncTask.class.getCanonicalName(), "doInBackground");
Thread.sleep(500);
//bject = "new object";
} catch (Exception exc) {
Log.e(CustomAsyncTask.class.getCanonicalName(), "exception");
object = null;
}
return object;
}
@Override
protected void onPostExecute(String s) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (s != null) {
Toast.makeText(context, "finished successfully!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "finished unsuccessfully!", Toast.LENGTH_LONG).show();
}
}
}
}
例2
这里,AsyncTask有点不同,execute方法接收要在后台分析的数据列表。返回结果取决于此检查。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
executeAsyncTaskOperation();
}
});
}
private void executeAsyncTaskOperation() {
Boolean[] bools = new Boolean[10];
for (int k = 0; k < 10; k++) {
if (k % 2 == 0) {
bools[k] = true;
} else {
bools[k] = false;
}
}
new CustomAsyncTask(this).execute(bools);
}
private static class CustomAsyncTask extends AsyncTask<Boolean, Void, Integer> {
private Context context;
private ProgressDialog progressDialog;
public CustomAsyncTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(context, "Please wait...", "Loading data from web");
}
@Override
protected Integer doInBackground(Boolean... params) {
int count = 0;
try {
Thread.sleep(1000);
Log.d(CustomAsyncTask.class.getCanonicalName(), "doInBackground");
for (Boolean param : params) {
if (param) {
count++;
}
}
} catch (Exception exc) {
Log.e(CustomAsyncTask.class.getCanonicalName(), "exception");
count = 0;
}
return count;
}
@Override
protected void onPostExecute(Integer s) {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (s != null && s > 0) {
Toast.makeText(context, "finished loading: " + s + " tasks", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "finished unsuccessfully!", Toast.LENGTH_LONG).show();
}
}
}
}