0

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

.setExpirationDuration( 1000000 )

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

谢谢

4

1 回答 1

1

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

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
}
于 2017-04-28T21:55:24.563 回答