5

I use Geofences in my app, everything works fine except the removal of triggered geofences. I red the guide from the official documentation of Android but they don't explain how to remove a geofence inside of the IntentService.

Here is the code of the event handler of the service:

@Override
protected void onHandleIntent(Intent intent) 
{
    Log.e("GeofenceIntentService", "Location handled");

    if (LocationClient.hasError(intent)) 
    {
        int errorCode = LocationClient.getErrorCode(intent);
        Log.e("GeofenceIntentService", "Location Services error: " + Integer.toString(errorCode));
    } 
    else 
    {
        int transitionType = LocationClient.getGeofenceTransition(intent);
        if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER)
        {
            List <Geofence> triggerList = LocationClient.getTriggeringGeofences(intent);
            String[] triggerIds = new String[triggerList.size()];

            for (int i = 0; i < triggerIds.length; i++) 
            {
                // Store the Id of each geofence
                triggerIds[i] = triggerList.get(i).getRequestId();

                Picture p = PicturesManager.getById(triggerIds[i], getApplicationContext());
                   /* ... do a lot of work here ... */


            }

        } 
        else 
            Log.e("ReceiveTransitionsIntentService", "Geofence transition error: " + Integer.toString(transitionType));
    }
}

How can I delete the geofence after he got triggered ?

Gp2mv3
  • 1,435
  • 20
  • 33

2 Answers2

2

You can do something like this:

LocationServices.GeofencingApi.removeGeofences(
           mGoogleApiClient,
           // This is the same pending intent that was used in addGeofences().
           getGeofencePendingIntent()
).setResultCallback(this); // Result processed in onResult().

And your getGeofencePendingIntent() method can look like this:

/**
 * Gets a PendingIntent to send with the request to add or remove Geofences. Location Services
 * issues the Intent inside this PendingIntent whenever a geofence transition occurs for the
 * current list of geofences.
 *
 * @return A PendingIntent for the IntentService that handles geofence transitions.
 */
private PendingIntent getGeofencePendingIntent() {
    Log.d(TAG, "getGeofencePendingIntent()");
    // Reuse the PendingIntent if we already have it.
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(mContext, GeofenceIntentServiceStub.class);
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // addGeofences() and removeGeofences().
    mGeofencePendingIntent = PendingIntent.getService(mContext, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return mGeofencePendingIntent;
}
IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
1

You would proceed as you did when adding Geofences (create a LocationClient and wait for it to connect). Once it is connected, in the onConnected callback method, you would call removeGeofences on the LocationClient instance instead and pass it a list of request IDs you want to remove and an instance of OnRemoveGeofencesResultListener as a callback handler.

Of course, you must use the same request IDs you used when creating the GeoFence with GeoFence.Builder's setRequestId.

@Override
public void onConnected(Bundle arg0) {
    locationClient.removeGeofences(requestIDsList, 
    new OnRemoveGeofencesResultListener() {
    ...     
});
Jeshurun
  • 22,940
  • 6
  • 79
  • 92