到目前为止,我只能找到以毫秒为单位设置地理围栏的到期时间的方法。我希望有一个用户到达地理围栏,退出它然后地理围栏将过期。但我只能找到如何设置时间或将其设置为永不过期。
.setExpirationDuration( 1000000 )
这没有常数变量吗?也许必须通过删除处理它的意图中的地理围栏来完成它?
由于
答案 0 :(得分: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
}