Android BroadcastReceiver and Activity.onPause()

怎甘沉沦 提交于 2019-12-13 17:56:59

问题


The documentation for BroadcastReceiver says:

If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead).

I made an example of Activity A1 that has an inner BroadcastReceiver that updates A1 interface when a Service S1 makes a sendBroadcast. S1 spends around 8 seconds to finish.

When running A1 and hitting the home button for making A1 call onPause, "it still receives the intent from sendBroadcast" and updates the interface, am I missing something or the documentation is wrong?
Thanks


回答1:


The document meant to say that if you unregistered in onPause() then you won't receive broadcast intents when paused. If you do not unregistered then you would continue to receive broadcast intents. You unregistered in onDestroy(), but when the home key is pressed only onStop() is called and onDestroy() will not be called. Thus you continue to receive broadcast.



来源:https://stackoverflow.com/questions/16503977/android-broadcastreceiver-and-activity-onpause

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