AsyncTask 使用常见问题解答和一般说明
=> 我应该在哪里进行网络操作?我应该在哪里返回我获得的值?
一般来说,您应该在单独的线程 -> doInBackground();因为您不希望您的 UI 在网络操作花费时间时冻结。因此,您应该连接到您的服务或 .php 脚本或从内部获取数据的任何地方doInBackground()
方法。然后您还可以解析那里的数据并从解析后的数据返回doInBackground()
方法通过指定返回类型doInBackground()
满足你的愿望,更多关于那里的内容。这onPostExecute()
然后方法将收到您的返回值doInBackground()
并使用 UI 来表示它们。
=> AsyncTask ==> 这是如何工作的?
一般来说,AsyncTask
类看起来像这样,它只不过是一个具有 3 个不同的通用类泛型类型 https://www.tutorialspoint.com/java/java_generics.htm:
AsyncTask<Params, Progress, Result>
您可以指定参数的类型AsyncTask
需要,进度指示器的类型和结果的类型(返回类型
doInBackGround())。
这是一个示例AsyncTask
看起来像这样:
AsyncTask<String, Integer, Long>
我们的参数为 String 类型,进度为 Integer 类型,结果为 Long 类型(返回类型doInBackground()
). 您可以对参数、进度和结果使用任何您想要的类型。
private class DownloadFilesTask extends AsyncTask<String, Integer, Long> {
// these Strings / or String are / is the parameters of the task, that can be handed over via the excecute(params) method of AsyncTask
protected Long doInBackground(String... params) {
String param1 = params[0];
String param2 = params[1];
// and so on...
// do something with the parameters...
// be careful, this can easily result in a ArrayIndexOutOfBounds exception
// if you try to access more parameters than you handed over
long someLong;
int someInt;
// do something here with params
// the params could for example contain an url and you could download stuff using this url here
// the Integer variable is used for progress
publishProgress(someInt);
// once the data is downloaded (for example JSON data)
// parse the data and return it to the onPostExecute() method
// in this example the return data is simply a long value
// this could also be a list of your custom-objects, ...
return someLong;
}
// this is called whenever you call puhlishProgress(Integer), for example when updating a progressbar when downloading stuff
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
// the onPostexecute method receives the return type of doInBackGround()
protected void onPostExecute(Long result) {
// do something with the result, for example display the received Data in a ListView
// in this case, "result" would contain the "someLong" variable returned by doInBackground();
}
}
=> 如何使用AsyncTask?我怎样才能“调用”它?我怎样才能“执行”它?
在这种情况下,AsyncTask 采用 String 或 String Array 作为范围调用 AsyncTask 后,它将如下所示:(指定的参数在AsyncTask的execute(param)方法中使用)。
new DownloadFilesTask().execute("Somestring"); // some String as param
请注意,此调用没有返回值,您应该使用的唯一返回值是从doInBackground()
. 使用 onPostExecute() 方法一定要利用返回值。
另外要小心这行代码:(这个执行实际上会有返回值)
long myLong = new DownloadFilesTask().execute("somestring").get();
.get() 调用导致 UI 线程被阻塞(因此,如果操作时间超过几毫秒,UI 就会冻结)当 AsyncTask 正在执行时,因为执行does not发生在一个单独的线程中。如果删除对 .get() 的调用,它将异步执行。
=> 这个符号“execute(String...params)”是什么意思?
这是一种所谓的“varargs http://www.journaldev.com/1257/java-variable-arguments-or-java-varargs-explained“(变量参数)参数。为了简单起见,我只想说这意味着实际的未指定可以通过此参数传递给方法的值的数量,并且您传递给该方法的任何数量的值都将被视为该方法内的数组。因此,这个调用可能如下所示:
execute("param1");
但它也可能看起来像这样:
execute("param1", "param2");
甚至更多参数。假设我们还在谈论AsyncTask
,可以通过这种方式访问参数doInBackground(String... params)
method:
protected Long doInBackground(String... params) {
String str1 = params[0];
String str2 = params[1]; // be careful here, you can easily get an ArrayOutOfBoundsException
// do other stuff
}
您可以在此处阅读有关 AsyncTask 的更多信息:http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html
另请看一下这个 AsyncTask 示例:https://stackoverflow.com/a/9671602/1590502 https://stackoverflow.com/a/9671602/1590502