Why won't my BroadcastReceiver call other methods?

被刻印的时光 ゝ 提交于 2019-12-11 12:28:39

问题


I have a BroadcastReceiver that can successfully catch a broadcast, but if I try to call another method from it, it won't always work. Here's my setup:

private class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (working == true) {
            //toast to make sure we got here
            doWork();
        }
    }
}

The toast within the if gets called, but doWork doesn't execute. Has anyone dealt with a similar issue?


回答1:


The Broadcast receiver doesn't have the same context and life cycle as your application, you can't do a lot of normal stuff in it. All your supposed to do is handle the event and return as quick as possible. In other words, start a service, or notify the user.

From: http://developer.android.com/reference/android/content/BroadcastReceiver.html

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

In particular, you may not show a dialog or bind to a service from within a BroadcastReceiver. For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.




回答2:


Most likely it's not staying awake long enough. Are you using a WakeLock to keep Android from going back to sleep?

So, firstly you'll need to add this to your manifest:

<uses-permission android:name="android.permission.WAKE_LOCK" />

and then in your receiver //pseudo code except for the parts that matter...

 public WakeLock wl;
    @Override
    public void onReceive(c,i){
       wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, TAG);//this matters
       wl.getWakeLock().acquire(); //as does this


       //do whatever but DON'T do it here... call a Service like this...
       Intent i = new Intent(context, MyExcellentService.class);

        try {
    PendingIntent.getService(context, 0,i, 0).send();
         }catch(Exception e){}

       //hold on to wl in some way and release it in the service, not here,
       wl.getWakeLock().release();


    }

If you're going to use a service you'll need to register it in your manifest like so...

<service android:name="MyExcellentService" />

And I would extend the Application and hold on to the WakeLock there, so that you don't have to pass it around in more convoluted ways. You can just reach up into the Application and talk straight to it!

You'll want to look up the differences in the *kinds of wakeups PARTIAL_WAKE_LOCK will not keep the screen awake, for example, just the background processing.

Let me know if this works, I'm cutting and pasting from various places, so, I may be overlooking something.



来源:https://stackoverflow.com/questions/8045836/why-wont-my-broadcastreceiver-call-other-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!