Is it OK to unregister 'dynamic' BroadcastReceiver from receiver's own onReceive() method?

微笑、不失礼 提交于 2020-01-03 15:32:51

问题


That is, I have this BroadcastReceiver I create on the fly to listen for one broadast, after which I want it to unregister itself.

I haven't found any sample code that does it this way, but neither have I found any rule in the android online docs that forbids this. But I cannot let it hang around for as long as the activity, and it is in an anonymous class anyway, so the containing class does not even know the variable name.

That is, the code looks something like this:

myInfoReceiver = new BroadcastReceiver() {
onReceive(Context ctx, Intent intt) {
    // do some Notification when I get here
    nm.notify("I got here") // obvious pseudo code
    ctx.unregisterReceiver(myInfoReceiver);
} // end onReceive
ctx.registerReceiver),uInfoReceiver, new IntentFilter(...));
}; // end BroadcastReceiver

But when I run this, Android complains when it calls unregister, insisting that the receiver is not there to unregister (I forget the exact wording, but it threw IllegalArgumentException).

I also tried modifying the code to check that the action in 'intt' is the same as expected -- but then it still executest onReceive but silently fails to unregister.


回答1:


The answer to your question is "yes". However...

...you need to call unregisterReceiver() on the same Context that you called registerReceiver() on. In the code you posted you are calling unregisterReceiver() on the Context passed in as an argument to onReceive(). This is not the same Context which is why you are getting the exception.




回答2:


I would simply say YES, no problem at all.

I use it for a one-time location fix for example, and can be used in other logics too without me seeing any problem to it.

Plus I've seen it around many times.




回答3:


I have tried various solutions, finally I am doing it that way:

Registering:

MyApplication.getInstance().getApplicationContext().registerReceiver(sentReceiver, new IntentFilter(SENT));

SentReceiver:

public class SentReceiver extends BroadcastReceiver  {
    public void onReceive(Context context, Intent arg1) {
        switch (getResultCode()) {
            case Activity.RESULT_OK:
                Toast.makeText(context,
                        context.getString(R.string.sms_envoye), Toast.LENGTH_SHORT)
                        .show();
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                Toast.makeText(context,
                        context.getString(R.string.sms_defaillance_generique),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NO_SERVICE:
                Toast.makeText(context,
                        context.getString(R.string.sms_pas_de_service),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_NULL_PDU:
                Toast.makeText(context,
                        context.getString(R.string.sms_pas_de_pdu),
                        Toast.LENGTH_SHORT).show();
                break;
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                Toast.makeText(context,
                        context.getString(R.string.sms_radio_desactivee),
                        Toast.LENGTH_SHORT).show();
                break;
        }
        MyApplication.getInstance().getApplicationContext().unregisterReceiver(this);
    }

With MyApplication:

public class MyApplication extends Application {
    private static MyApplication mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized MyApplication getInstance() {
        return mInstance;
    }        
}


来源:https://stackoverflow.com/questions/30783282/is-it-ok-to-unregister-dynamic-broadcastreceiver-from-receivers-own-onreceive

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