Android AsyncTask 示例及说明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25647881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android AsyncTask example and explanation
提问by Suragch
I want to use an AsyncTask
in my app, but I am having trouble finding a code snippet with a simple explanation of how things work. I just want something to help me get back up to speed quickly without having to wade through the documentationor lots of Q&As again.
我想AsyncTask
在我的应用程序中使用 ,但我无法找到一个代码片段,其中包含对事物如何工作的简单解释。我只是想要一些东西来帮助我快速恢复速度,而不必再次浏览文档或大量问答。
回答by Suragch
AsyncTask
is one of the easiest ways to implement parallelism in Android without having to deal with more complex methods like Threads. Though it offers a basic level of parallelism with the UI thread, it should not be used for longer operations (of, say, not more than 2 seconds).
AsyncTask
是在 Android 中实现并行性的最简单方法之一,而无需处理更复杂的方法,如线程。尽管它提供了与 UI 线程的基本并行级别,但不应将其用于较长的操作(例如,不超过 2 秒)。
AsyncTask has four methods
AsyncTask 有四种方法
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
onPreExecute()
doInBackground()
onProgressUpdate()
onPostExecute()
where doInBackground()
is the most important as it is where background computations are performed.
哪里doInBackground()
是最重要的,因为它是执行后台计算的地方。
Code:
代码:
Here is a skeletal code outline with explanations:
这是一个带有解释的骨架代码大纲:
public class AsyncTaskTestActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// This starts the AsyncTask
// Doesn't need to be in onCreate()
new MyTask().execute("my string parameter");
}
// Here is the AsyncTask class:
//
// AsyncTask<Params, Progress, Result>.
// Params – the type (Object/primitive) you pass to the AsyncTask from .execute()
// Progress – the type that gets passed to onProgressUpdate()
// Result – the type returns from doInBackground()
// Any of them can be String, Integer, Void, etc.
private class MyTask extends AsyncTask<String, Integer, String> {
// Runs in UI before background thread is called
@Override
protected void onPreExecute() {
super.onPreExecute();
// Do something like display a progress bar
}
// This is run in a background thread
@Override
protected String doInBackground(String... params) {
// get the string from params, which is an array
String myString = params[0];
// Do something that takes a long time, for example:
for (int i = 0; i <= 100; i++) {
// Do things
// Call this to update your progress
publishProgress(i);
}
return "this string is passed to onPostExecute";
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Do things like hide the progress bar or change a TextView
}
}
}
Flow Diagram:
流程图:
Here is a diagram to help explain where all the parameters and types are going:
这是一个图表,可帮助解释所有参数和类型的去向:
Other helpful links:
其他有用的链接:
- What arguments are passed into AsyncTask<arg1, arg2, arg3>?
- Slidenerd Android AsyncTask Tutorial: Android Tutorial For Beginners
- Understanding AsyncTask – Once and Forever
- Dealing with AsyncTask and Screen Orientation
- How to pass multiple parameters to AsynkTask
- how to pass in two different data types to AsyncTask, Android