3

In the application I'm working on, one of the features is to notify the user when they reach a location that they have set prior.

The code below is in addProximityAlert in Activity:

final Intent intent = new Intent(PROX_ALERT_INTENT);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(
        InfoActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);
locationManager.addProximityAlert(18.7726271, 98.9738381, 5000, -1,
        pendingIntent);
this.locationReminderReceiver = new LocationReminderReceiver();
final IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
this.registerReceiver(this.locationReminderReceiver, filter);

@Override
protected void onPause() {
    super.onPause();
    if (this.locationReminderReceiver != null) {
        Log.i("unregisterReceiver", "unregisterReceiver");
        this.unregisterReceiver(this.locationReminderReceiver);
    }
}

Here's the receiver:

public class LocationReminderReceiver extends BroadcastReceiver {

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

    final String key = LocationManager.KEY_PROXIMITY_ENTERING;
    final Boolean entering = intent.getBooleanExtra(key, false);

    if (entering) {
        Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
        Log.i("LocationReminderReceiver", "entering");
    } else {
        Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
        Log.i("LocationReminderReceiver", "exiting");
    }
}
}

It works fine, but I need to call unregisterReceiver every single time I destroy the Activity - that means my application no longer notifies the user. But I want to notify the user when he is near the location until he cancels, or is already notified even if they close the app.

What am I missing?

Makoto
  • 104,088
  • 27
  • 192
  • 230
Intathep
  • 3,378
  • 2
  • 21
  • 28
  • I haven't used LocationManager much, but from the looks of your code, you are registering for a callback via broadcast with the system service - LocationService. Surely your application (the receiver) will be woken up when the location is reached? – Aswin Kumar Sep 28 '12 at 11:05
  • yes the receiver will be call when the location is reached as long as this activity still alive but i need to notify even if activity is dead – Intathep Sep 28 '12 at 11:19
  • Hi, I'm working on a similar app, been stuck for days! I would truly appreciate if you could share your codeee – Pedrum Mar 28 '14 at 06:15

2 Answers2

3

If the user closes your activity, you should indeed unregister your location listener.

It sounds like you need to move part of your application (the bit that monitors location and alerts the user) to a background service, so that it can continue to run even after the user closes the app.

logical Chimp
  • 996
  • 5
  • 7
  • i think so, i need to move some code to bg process do you have any idea for this ? i dont have any idea how to do this – Intathep Sep 28 '12 at 11:22
2

all i need to do is define receiver in Manifest

then i don't need to register/unregister in Activity anymore

  <receiver android:name="th.clbs.android.broadcastreceiver.LocationReminderReceiver" >
        <intent-filter>
            <action android:name="th.co.clbs.action.LOCATION_REMINDER" />
        </intent-filter>
    </receiver>
Intathep
  • 3,378
  • 2
  • 21
  • 28
  • I'm working on a similar app, I was wondering to do the action part which is LOCATION_REMINDER here. – Pedrum Mar 28 '14 at 06:14