0

I want to start an activity when a person enters/exits a geofence(email/sms app,contacts app etc).

  1. Is this possible?

  2. If this is possible, the documentation suggests the following

    The Intent sent from Location Services can trigger various actions in your app, but you should not have it start an activity or fragment, because components should only become visible in response to a user action. In many cases, an IntentService is a good way to handle the intent.

  3. Can we consider the entering a geofence as an user action?

halfer
  • 19,824
  • 17
  • 99
  • 186
Droidme
  • 1,223
  • 6
  • 25
  • 45

1 Answers1

1
    Intent intent = new Intent("alert");
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
    locationManager.addProximityAlert(latitude, 
            longitude, PROXIMITY_DISTANCE, -1, proximityIntent);

Then register a receiver that extends BroadcastReceiver

IntentFilter filter = new IntentFilter("alert");
registerReceiver(<your receiver>, filter);

There you can do your thing in the onReceive method

 if (intent != null) {
     String key = LocationManager.KEY_PROXIMITY_ENTERING;
     boolean entering = intent.getBooleanExtra(key, false);
     if (entering) {
         // whatever
     }
 }
Carsten
  • 806
  • 5
  • 6