BroadcastReceiver does not receive Broadcast from IntentService

后端 未结 1 1462
予麋鹿
予麋鹿 2021-01-25 14:06

I\'m trying to send a broadcast from an IntentService to the activity that started it, this is how i register the Receiver in the activity:

private BroadcastRece         


        
相关标签:
1条回答
  • 2021-01-25 14:19

    You should register and unregister your receivers in onResume() and in onPause() respectively. Cause if you only register it in onCreate() and unregister in onPause(), then the next time the activity is brought to the foreground, onCreate() will not be called again and then it will not register the receiver again. But onResume() is always called on the activity being displayed.

    public void onResume() {
        super.onResume();
        ....
        registerReceiver(myBroadcastReceiver, intentFilter);
    }
    
    public void onPause() {
        super.onPause();
        ...
        unregisterReceiver(myBroadcastReceiver);
    }
    
    0 讨论(0)
提交回复
热议问题