What is the best way of create BroadcastReceiver?

烈酒焚心 提交于 2019-12-01 10:30:53
Vineet Shukla

Does the OS kill BroadcastReceiver if it idle too long? Does the OS NOT kill the BroadcastReceiver if it keeps receiving broadcast?

Will the BroadcastReceiver (Method 1 and 2) killed by OS when it is under cases of extreme memory pressure?

Your receiver comes in action when it receives any notification and it's active duration is it's onReceive() Method.

For Method 2, is it possible that the LocationBoardcastReceiver.java send data to a running activity (Eg: Main,java)?

Bad idea.

For Method 1, is it any way to keep the BroadcastReceiver alive during the life time of the Main.java (Main Activity)?

Check above.

Your best way will depend over your app what it requires.

  1. If your app wants to use BroadcastReceiver while app is in foreground then you should go for Method1 as you have mentioned.

  2. If your app need to receive system's notification as Boot Completion etc then you should go for Method2 as you have mentioned.

  1. Android OS can kill Services and Activities any time it pleases. There is nothing you can do about it.

  2. You can set broadcast receiver to start a service even if app is not active.

  3. If you want to receive location updates ALL THE TIME, you will need to have device awake all the time. This will drain battery in the matter of hours.

For best practices on location updates read this: http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html?m=1

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