2

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
  • Knickedi, thanks for reminding. By the way, do you have any idea about my question? Oct 3, 2011 at 14:50

2 Answers 2

3
  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

0
2

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
  • There is one important point that I want to clarify. For Method 1, Will the registered BroadcastReceiver kill by the OS during that Activity life time? If yes, how can I know when it killed? Because I have to make sure the BroadcastReceiver (at Main.java) can receive data from Service anytime. Thanks Oct 3, 2011 at 14:58
  • your BroadcastReceiver will not get killed but your app can get killed by OS if it needs the resources, your app using. Oct 3, 2011 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.