Question

I am wondering what happens when you create a broadcast reciever from within your main activity (in my case a proximity alert receiver), and the app process is killed for some unknown reason?

I want my proximity alerts to be received in the broadcast recevier i register, regardless of my app state, will that happen or do i need to do something in particular to ensure that?

EDIT to clarify:

I have to register the receiver from within my app rather than via the manifest. Since i want multiple proximityalerts, for each (varying) location i will need to create the receivers dynamically, since i need to register the receiver for each location, with a unique id, unfortunately.

code to create my intent/pendingintent/broadcastreceiver:

    double latitude = location.getLat();
    double longitude = location.getLon();
    Intent intent = new Intent(PROX_ALERT_INTENT_ID);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(activity.getApplicationContext(), 0, intent, 0);
    lm.addProximityAlert(
        latitude, // the latitude of the central point of the alert region
        longitude, // the longitude of the central point of the alert region
        POINT_RADIUS, // the radius of the central point of the alert region, in meters
        PROX_ALERT_EXPIRATION, // time for this proximity alert, in milliseconds, or -1 to indicate no                           expiration
        proximityIntent // will be used to generate an Intent to fire when entry to or exit from the alert region is detected
    );

    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT_ID);

    activity.registerReceiver(new ProximityIntentReceiver(location), filter);
Was it helpful?

Solution

If you want to have your BroadcastReceivers triggered regardless of your apps state then you should register them through your applications AndroidManifest.xml file.

Heres how to do it.

  1. Define a class that extends BroadcastReceiver and implement the onReceive() method. I see that you have already done that - ProximityIntentReceiver is that class.

  2. In your AndroidManifest.xml file add:

    <application>
    ...
        <receiver
            android:name=".MyReceiver"
            android:exported="false" >
            <intent-filter>
                   <action android:name="my.app.ACTION" />
            </intent-filter>
        </receiver> 
    </application>
    

Where MyReceiver is the name of your receiver class (ProximityIntentReceiver in your case) and my.app.ACTION is the action that your receiver will listen for (in your case I'm guessing it's the value of PROX_ALERT_INTENT_ID).

Note: Saying that the name of your receiver is .MyReceiver assumes that it's located in the root package of your app. If that is not the case then you need to provide the path to that class starting from the root.

OTHER TIPS

@Mathias: The way I registered a broadcast receiver dynamically and being active even when the app is killed is by run a service and register the receiver from there. Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top