我在使用 AsyncTask 的 OnPOstExecute 时遇到问题,它运行良好,但如果应用程序停止运行,则会生成一个通知,该通知会打开运行 AsyncTask 的 Activity,但永远不会调用 OnPOstExecute。
我的疑问是:如果应用程序已关闭,当我尝试从通知中打开活动时,为什么 OnPOstExecute 不起作用。
代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
...
try{
loadListTask = new LoadListTask();
loadListTask.execute();
}catch(Exception e){
Log.d(TAG,"ERRO: "+e.getMessage());
}
}
public void loadListView() {
messagesDb = useful.buscarMessagesDb(this);
ArrayList<Message> messageListViews = new ArrayList<Message>();
int messagesDbSize = messagesDb.size();
for (int i = 0; i < messagesDbSize; i++) {
if (((messagesDb.get(i).idFrom == myId) && (messagesDb.get(i).idTo == idContact)) ||
((messagesDb.get(i).idFrom == idContact) && (messagesDb.get(i).idTo == myId))) {
Message message = new Message(messagesDb.get(i).idFrom, messagesDb.get(i).idTo,
messagesDb.get(i).dt, messagesDb.get(i).mensagem, messagesDb.get(i).confirm,
messagesDb.get(i).idWeb);
messageListViews.add(message);
} else {
//Log.d(TAG, "Essa mensagem não é pra esse contato!!!");
}
}
listChat.setAdapter(null);
pref.edit().putBoolean("isUpdate", false).commit();
AdapterMessageListView adapterMessageListView = new AdapterMessageListView(myId, idContact,
getApplicationContext(), messageListViews);
listChat.setAdapter(adapterMessageListView);
listChat.setCacheColorHint(Color.TRANSPARENT);
listChat.setSelection(listChat.getCount());
}
private class LoadListTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
try {
Log.d(TAG,"doInBackground");
Thread.sleep(100);
} catch (InterruptedException e) {
}
return "list";
}
@Override
public void onPreExecute() {
Log.d(TAG,"PREEXECUTE");
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG,"PostExecute");
loadListView();
loadListTask = new LoadListTask();
loadListTask.execute();
super.onPostExecute(result);
}
}
信息:
02-25 14:05:42.396: D/Chat(20663): doInBackground
02-25 14:05:42.396: W/MessageQueue(20663): Handler{40532ad8} sending message to a Handler on a dead thread
02-25 14:05:42.396: W/MessageQueue(20663): java.lang.RuntimeException: Handler{40532ad8} sending message to a Handler on a dead thread