Android BroadcastReceiver onReceive() called twice on android 4.0

前端 未结 15 2274
梦谈多话
梦谈多话 2020-12-29 20:38

I faced to one problem on android 4.0.3 (on 4.1.2 it works fine). I have in my Activity BroadcastReceiver. When I send a broadcast, method onReceive() called always twice. P

15条回答
  •  自闭症患者
    2020-12-29 20:53

    I feel that it would be best to register the receiver after you have overridden the BroadcastReceiver and before starting the intent from which you want to receive data.(Otherwise it may throw a NullPointerException.
    Unregister the reciever in onStop() rather than in onPause().

    I have noticed that unregistering the receiver in the onPause() creates problems if you have services or background tasks to be executed, which will pause the activity and hence unregister your Broadcast Receiver. (This will result in multiple calls to the onRecieve() method, i.e. each time the receiver is re-registered.)

提交回复
热议问题