有没有办法在 android 退出后让地理围栏过期

Is there a way to expire a geofence once exited on android

到目前为止,在网上我只能找到以毫秒为单位设置地理围栏到期时间的方法。我想让用户到达地理围栏,退出它,然后地理围栏就会过期。但我只能找到如何设置时间或将其设置为永不过期。

.setExpirationDuration( 1000000 )

这个没有常量变量吗?也许必须通过删除处理它的意图中的地理围栏来完成?

谢谢

如果您只关心在退出事件时设置超时,只需在触发退出时更新该地理围栏。这是如何执行此操作的一个非常基本的示例,并附有有关如何改进它的注释。一旦你开始工作,你基本上可以让地理围栏保持活动状态,只有当它们在一段时间内没有被触发时才会超时。

public void onReceive(Intent intent) {
    //.... your code ....

    GeofencingEvent event = GeofencingEvent.fromIntent(intent);

    if (event.getGeofenceTransition() == GEOFENCE_TRANSITION_EXIT) {
        //This assumes you only worry about one geofence, but there may be others in this list...
        List<Geofence> geofences = event.getTriggeringGeofences();
        Geofence geofence = geofences.get(0);
        String id = geofence.getRequestId(); //Do a check to see if this is the geofence you want to modify.

        //Either send the id to the class that handles adding geofences, or have a reference to your GoogleApiClient here.

        //Assuming you have GoogleApiClient reference here...
        Geofence.Builder geofenceBuilder = new Geofence.Builder();
        geofenceBuilder.setCircularRegion(lat, lon, radius);
        geofenceBuilder.setExpiration(1000000);
        geofenceBuilder.setRequestId(id);
        geofenceBuilder.setTransitionTypes(transitionTypes);

        //The GeofencingRequest class is parcelable too, so this be another option to send your request to a class that handles google api calls.
        GeofencingRequest.Builder requestBuilder = new GeofencingRequest.Builder();
        requestBuilder.addGeofence(geofenceBuilder.build());
        requestBuilder.setInitialTrigger(initialTrigger);
        LocationServices.GeofencingApi.addGeofences(apiClient, requestBuilder.build(), pendingIntent);
    }

    //... the rest of your stuff
}