AsyncTask 从不执行 onPostExecute
我正在尝试执行以下 AsyncTask :
private class TimeUpdateTask extends AsyncTask<List<mObject>, Integer, Void> {
@Override
protected Void doInBackground(List<mObject>... params) {
mObject o;
int i;
int numChecked = 0;
List<mObject> mObjects = params[0];
while(true)
{
if (isCancelled())
{
Log.w("TAG", "task interrumped");
return null;
}
for (i=0 ; i < mObjects.size() ; i++)
{
o = mObjects.get(i);
if (!o.isChecked())
{
o.calculateProgress();
if (o.isChecked())
{
numChecked++;
}
}
}
publishProgress(numChecked);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
@Override
protected void onProgressUpdate(Integer... param){
int progressCompleted = param[0];
((ArrayAdapter<mObject>) EventListView.this.EventView.getAdapter()).notifyDataSetChanged();
mMain.setProgressBarCompleted(progressCompleted);
}
/*This should execute*/
@Override
protected void onPostExecute(Void result) {
Log.d("TAG","End onPostExecute");
}
}
因此,当我对此任务调用 cancel(false)
时,应该执行 onPostExecute
但它从未执行过。
是代码有问题还是其他什么问题? 我在 SO 中查看了很多答案,似乎有 #5 AsyncTask
挂在身边是正常的,即使您目前只使用一个(如我的情况)。
I am trying to execute the following AsyncTask :
private class TimeUpdateTask extends AsyncTask<List<mObject>, Integer, Void> {
@Override
protected Void doInBackground(List<mObject>... params) {
mObject o;
int i;
int numChecked = 0;
List<mObject> mObjects = params[0];
while(true)
{
if (isCancelled())
{
Log.w("TAG", "task interrumped");
return null;
}
for (i=0 ; i < mObjects.size() ; i++)
{
o = mObjects.get(i);
if (!o.isChecked())
{
o.calculateProgress();
if (o.isChecked())
{
numChecked++;
}
}
}
publishProgress(numChecked);
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
@SuppressWarnings("unchecked")
@Override
protected void onProgressUpdate(Integer... param){
int progressCompleted = param[0];
((ArrayAdapter<mObject>) EventListView.this.EventView.getAdapter()).notifyDataSetChanged();
mMain.setProgressBarCompleted(progressCompleted);
}
/*This should execute*/
@Override
protected void onPostExecute(Void result) {
Log.d("TAG","End onPostExecute");
}
}
So when I call cancel(false)
on this task, onPostExecute
should be executed but it never is.
Is there any problem in the code or something else?
I have looked at a lot of answers in SO and it seems that having #5 AsyncTask
hanging around is normal, even if you are using only one (as in my case) at the moment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里缺少一大块你必须了解的内容。
来自开发人员文档(请参阅下面的粗体文本): http:// developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
There is 1 large piece missing here that you must learn about.
from the developer docs (see bold text below): http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean)
Android使用onCancelled()当任务被取消时,它不会调用onPostExecute!
Android use onCancelled() when the task is cancelled, it don't call onPostExecute!
我认为调用 cancel 实际上取消了所有执行,包括 onPostExecute。您应该使用自己的本地布尔变量并执行以下操作:
然后在循环中执行以下操作:
I think calling cancel actually cancels everything from executing, including the onPostExecute. You should use your own local boolean variable and do this:
then in your loop do this: