开发者

How to wake up/start not running app from c2dm listener on Android?

i am running into issues during testing when my app is not running in the background. I broadcast c2dm message to it, it does received it, i the service performs AsyncTask to grab some data off my server, all is good until it fails on "onPostExecute" with a MessageQueue error:

sending message to a Handler on a dead thread

everything works fine when the app is running in the background, but fails when it's not. do i have to wake it up somehow? thanks!

UPDATE: here is the snippet of my 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...
    }       
 }

UPDATE #2: Error stack trace

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(Thre开发者_如何学JAVAadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)


Are you firing up an AsyncTask from within a broadcast receiver? If so, you'll likely want to move your AsyncTask to a service, and start the service from the receiver. Broadcast receivers aren't intended to be used for much more than very basic book-keeping or just starting a service or activity.

EDIT: Also consider using the very handy IntentService class, which handles its intent in a separate thread and stops itself when it's done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜