转换后删除触发的地理围栏

Max*_*all 1 android transition android-geofence

我试图在输入后删除地理围栏,以阻止它重新触发输入转换.从类似的问题我得到这条线,效果很好

LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient,getGeofencePendingIntent()).setResultCallback(this);
Run Code Online (Sandbox Code Playgroud)

但是它会删除所有地理围栏,而不仅仅是已触发的地理围栏.

为了选择要删除的正确ID,我需要做什么?

Mim*_*oli 6

您需要传递用于构建Geofence的相同String.

String geofenceId = "randomId";
Geofence geofence = new Geofence.Builder()
    .setRequestId(geofenceId)
    ....
    .build();

GeofencingRequest request = new GeofencingRequest.Builder()
    .addGeofence(geofence)
    ....
    .build();

LocationServices.GeofencingApi.addGeofences(mGoogleApiClient, request, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

要删除地理围栏,您可以使用

List<String> geofencesToRemove = new ArrayList<>();
geofencesToRemove.add(geofenceId);
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, geofencesToRemove);
Run Code Online (Sandbox Code Playgroud)

或者你可以从你收到的意图中获得地理围栏.

GeofencingEvent event = GeofencingEvent.fromIntent( intent_you_got_from_geofence );
List<Geofence> triggeredGeofences = event.getTriggeringGeofences();
List<String> toRemove = new ArrayList<>();
for (Geofence geofence : triggeredGeofences) {
    toRemove.add(geofence.getRequestId());
}
LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, toRemove);
Run Code Online (Sandbox Code Playgroud)