What is the best way of create BroadcastReceiver?

懵懂的女人 提交于 2019-12-01 09:37:28

问题


I have spent few days to work on Service + BroadcastReceiver, but still cannot make it perfect. I hope someone can help, thanks!

I am writing a App that show user current location on map(The map that I wrote, not Google Map) and send out Notification alarm when user go inside a predefined zone.

In my code. there are two main objects. A GPS service and Main Activity.

The GPS service broadcast location when location changed. The Main Activity receive the latest location by BroadcastReceiver.

I have done some researches on how to register BroadcastReceiver. There are two ways that I found:


Method 1 - Register Broadcast Receiver inside Activity (I am using this method in my code. I need to update latest location on map)

Main.java:

public class Main extends Activity{
.
.
.
    public class MyLocReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub

            Bundle bundle=intent.getExtras();
            String locData = bundle.getString("loc");

            // Some work


        }
        public MyLocReceiver(){
            Log.e(TAG, "in MyReceiver()");
        }

    }

    private MyLocReceiver myLocReceiver;

    // Register BroadcastReceiver
    if (myLocReceiver == null)
        {
            Common.writeFile("service.txt", "Main - in myBindService() register receiver" + "\n", true);

            myLocReceiver=new MyLocReceiver();

            registerReceiver(myLocReceiver, new IntentFilter("com.nwfb.LOC_DATA"));
        }

Method 2 - Register BroadcastReceiver at Manifest.xml

Manifest.xml:

    <receiver android:name=".LocationBoardcastReceiver" android:enabled="true"> 
            <intent-filter>
                <action android:name="com.abc.LOC_DATA" /> 
            </intent-filter> 
    </receiver>

LocationBoardcastReceiver.java:

    public class LocationBoardcastReceiver extends BroadcastReceiver {
        private static final String TAG = LocationBoardcastReceiver.class.getSimpleName();

        @Override
        public void onReceive(Context context, Intent intent) {

        Bundle bundle=intent.getExtras();
        // Do something
        }
    }

* What I want is the BroadcastReceiver MUST NOT KILLED by the OS in the application life time. Also the BroadcastReceiver must able to pass data to my Main.java

I read this actical: http://developer.android.com/reference/android/content/BroadcastReceiver.html

The section of 'Receiver Lifecycle' and 'Process Lifecycle' state that the BroadcastReceiver will be finished and no longer active after onReceive(Context, Intent) called.

I am using Method 1, I can receive more than 1 Location data from the Service. I found that the BroadcastReceiver keeps alive if the BroadcastReceiver keeps receiving data from the Broadcast. If I turn off GPS at 'Setting -> Location ' and let the BroadcastReceiver idle for about 1 to 2 hours, then the BroadcastReceiver will killed by OS.


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?

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

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


回答1:


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.




回答2:


  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



来源:https://stackoverflow.com/questions/7636273/what-is-the-best-way-of-create-broadcastreceiver

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