当我的应用程序未在后台运行时,我在测试期间遇到了问题。我向它广播 c2dm 消息,它确实收到了它,我服务执行 AsyncTask 以从我的服务器上获取一些数据,一切都很好,直到它在“onPostExecute”上失败并出现 MessageQueue 错误:
在死线程上向 Handler 发送消息
当应用程序在后台运行时一切正常,但在后台运行时会失败。我必须以某种方式唤醒它吗?谢谢!
更新:这是我的 AsyncTask 的片段:
private class getNewMessages extends AsyncTask<Void, Void, Boolean> {
private Context gContext;
private String gToken;
private int gNewMessages;
public getNewMessages(Context context, String token) {
super();
gContext = context;
gToken = token;
}
protected Boolean doInBackground(Void... args) {
gNewMessages = data.serverNewMessagesGet(gContext, gToken);
return true;
}
protected void onPostExecute(Boolean result) {
Log.d(common.DEBUG_TAG, "retrieved " + gNewMessages + " new messages");
// notify user, etc...
}
}
更新#2:错误堆栈跟踪
Handler{4585c038} sending message to a Handler on a dead thread
java.lang.RuntimeException: Handler{4585c038} sending message to a Handler on a dead thread
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:184)
at android.os.Handler.sendMessageAtTime(Handler.java:457)
at android.os.Handler.sendMessageDelayed(Handler.java:430)
at android.os.Handler.sendMessage(Handler.java:367)
at android.os.Message.sendToTarget(Message.java:348)
at android.os.AsyncTask$3.done(AsyncTask.java:214)
at java.util.concurrent.FutureTask$Sync.innerSet(FutureTask.java:252)
at java.util.concurrent.FutureTask.set(FutureTask.java:112)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:310)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)