[英]Android app: Calling AsyncTask twice?
I'm using AsyncTask and some pretty common Android code to get the contents of a remote webpage. 我正在使用AsyncTask和一些非常常见的Android代码来获取远程网页的内容。 Based on that returned content, I can then call another page.
根据返回的内容,我可以调用另一个页面。
http://developer.android.com/reference/android/os/AsyncTask.html http://developer.android.com/reference/android/os/AsyncTask.html
My debugging lines should print like this:
我的调试行应该像这样打印:
1> StartA()
2> onPreExecute
3> doInBackground
4> onPostExecute Note: Code here will call EndA()
5> EndA()
6>
7> StartB()
8> onPreExecute
9> doInBackground
10> onPostExecute Note: Code here will call EndB()
11> EndB()
Is that impossible to do? 这不可能吗? I get all of the above to work... EXCEPT I get one addtional call to EndB() appearing between lines 8 and 9.
我得到了以上所有内容...除了我在第8行和第9行之间出现一个对EndB()的附加调用。
I can't for the life of me figure out why. 我不能为我的生活找出原因。 Nothing looks like it should call EndB() twice.
没有什么看起来应该两次调用EndB()。 And it definitely shouldn't get called BEFORE 9 and 10.
它绝对不应该在9和10之前被称为。
private void StartA()
{
Debug("StartA()");
g_GetWhat = 1;
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://google.com" });
}
private void EndA()
{
Debug("EndA()");
StartB();
}
private void StartB()
{
Debug("StartB()");
g_GetWhat = 2;
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://yahoo.com" });
}
private void EndB()
{
Debug("EndB()");
}
/////////////////////////////////////////////////// ////////////////////////////////////////////////// /
private class DownloadWebPageTask extends AsyncTask<String, Void, String>
{
protected void onPreExecute()
{
Debug("onPreExecute()");
}
protected String doInBackground(String... urls)
{
Debug("doInBackground()");
}
protected void onPostExecute(String result)
{
Debug("onPostExecute()");
if(g_GetWhat == 1) { EndA(); }
if(g_GetWhat == 2) { EndB(); }
}
}
You can only execute an AsyncTask
instance once. 您只能执行一次
AsyncTask
实例。 You are actually creating two instances, but you should call it like this anyways so that it can never be recalled: 你实际上是在创建两个实例,但你应该这样调用它,以便它永远不会被召回:
new DownloadWebPageTask().execute(new String[] { "http://yahoo.com" });
new DownloadWebPageTask().execute(new String[] { "http://google.com" });
instead of like this: 而不是像这样:
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://google.com" });
I think your running into the problem here: 我想你在这里遇到了问题:
private void EndA()
{
Debug("EndA()");
StartB();
}
Your value for g_GetWhat
is getting changed as soon as StartB begins. 一旦StartB开始,你的
g_GetWhat
值g_GetWhat
改变。 So when execution returns from EndA()
the next if statement evaluates to true since g_GetWhat
's value has changed. 因此,当执行从
EndA()
返回时,下一个if语句的计算结果为true,因为g_GetWhat
的值已更改。
if(g_GetWhat == 1) { EndA(); }
if(g_GetWhat == 2) { EndB(); }
The value for g_GetWhat
is actually 2, which is why you see the result you are seeing. g_GetWhat
的值实际为2,这就是您看到结果的原因。 You should pass g_GetWhat into your AsyncTask when you call it and make it an instance variable of the task. 您应该在调用它时将g_GetWhat传递给AsyncTask,并使其成为任务的实例变量。
如果您需要在后台同时进行多个工作,那么您应该在Android中使用Service类(而不是IntentService,因为调用已入队并且不会同时运行)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.