我最近更新了Google-Play-Service库,因为一些地理围栏和GCM所需的类已被删除/弃用。我已经从Creating and Monitoring Geofences下载了地理栅栏的示例代码
示例代码只监视入口和出口,我所做的是在我的IntentService中添加Geofence.GEOFENCE_TRANSITION_DWELL并设置游荡时间,我提供了逻辑来处理
geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL进入/退出时的Geofence工作正常,但Dwell_TIME未触发。我已将geofences设置为
mGeofenceList.add(new Geofence.Builder().setRequestId(geo_cord.getJSONObject(i_arrcords).getString("store_id"))
.setCircularRegion(
Double.valueOf(geo_cord.getJSONObject(i_arrcords).getString("lat")),
Double.valueOf(geo_cord.getJSONObject(i_arrcords).getString("longtd")),
Float.valueOf(geo_cord.getJSONObject(i_arrcords).getString("radius"))
)
.setExpirationDuration(Constants.NEVER_EXPIRE)
.setLoiteringDelay( Integer.valueOf(geo_cord.getJSONObject(i_arrcords).getString("dwelltime")) )
.setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_DWELL |
Geofence.GEOFENCE_TRANSITION_EXIT)
.build());我的意图服务为
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()) {
String errorMessage = GeofenceErrorMessages.getErrorString(this,
geofencingEvent.getErrorCode());
Log.d(TAG, errorMessage);
return;
}
// Get the transition type.
int geofenceTransition = geofencingEvent.getGeofenceTransition();
if ((geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL))
{
Log.d(app_tag,GeoStrings.geofence_transition_dwell);
task_geo_dwell task_geofence_d=new task_geo_dwell();
task_geofence_d.execute();
}不幸的是,我在日志中没有得到任何输出,GEOFENCE_TRANSITION_DWELL也从未触发过。请帮帮我。提前谢谢你
发布于 2017-01-01 15:16:51
仅当您进入地理围栏并在离开地理围栏之前使用setLoiteringDelay在那里停留设置的时间时,才会触发GEOFENCE_TRANSITION_DWELL。
另一点是,您必须将初始触发选项指定为GeofencingRequest.INITIAL_TRIGGER_DWELL和GeofencingRequest.INITIAL_TRIGGER_ENTER。检查下面的代码:
try {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER |GeofencingRequest.INITIAL_TRIGGER_DWELL);
final Geofence geofence= new Geofence.Builder()
.setRequestId("geofence1")
.setCircularRegion(lat, lng,radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
Geofence.GEOFENCE_TRANSITION_EXIT |
Geofence.GEOFENCE_TRANSITION_DWELL)
.setLoiteringDelay(20000)
.build();
builder.addGeofence(geofence);
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
builder.build(),
getGeofencePendingIntent()
).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
if (status.isSuccess())
{
//do something to show geofence added success;
}
}
});
} catch (SecurityException securityException) {
}发布于 2016-03-29 22:40:13
你能分享一下你创建geofence请求的代码吗?您还必须将初始触发器设置为驻留,如下所示:
GeofencingRequest getGeofencingRequest() {
GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
//triggers events only when the user stops for a defined duration within a geofence.
builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
builder.addGeofences(mGeofenceList);
return builder.build();
}geofence请求是addGeofences()接口的一个参数。例如:
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
);发布于 2016-02-05 22:11:01
我怀疑如果GEOFENCE_TRANSITION_ENTER或GEOFENCE_TRANSITION_EXIT已经被触发到相同的GeoFence对象,则GEOFENCE_TRANSITION_DWELL不会触发。
https://stackoverflow.com/questions/31917062
复制相似问题