我最近更新了Google-Play-Service Library,因为某些类需要进行地理围栏,并且GCM已被删除/弃用。我已经下载了geofencing的示例代码 Creating and Monitoring Geofences
示例代码只监视Entry和Exit,我所做的是添加了Geofence.GEOFENCE_TRANSITION_DWELL并在我的IntentService中设置了游荡时间我提供的逻辑来处理
geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL
Enter / Exit上的地理栅栏工作正常,但Dwell_TIME未触发。 我将地理围栏设置为
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
从未触发过。请帮帮我。
提前谢谢
答案 0 :(得分:2)
GEOFENCE_TRANSITION_DWELL仅在您输入地理围栏并在退出地理围栏之前在使用setLoiteringDelay设置的时间内停留时触发。
另一点是你必须指定初始触发选项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) {
}
答案 1 :(得分:1)
您可以分享创建地理围栏请求的代码吗?您还必须将初始触发器设置为DWELL,如下所示:
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();
}
地理围栏请求是addGeofences()api的参数。例如:
LocationServices.GeofencingApi.addGeofences(
mGoogleApiClient,
getGeofencingRequest(),
getGeofencePendingIntent()
);
答案 2 :(得分:-3)
我怀疑如果GEOFENCE_TRANSITION_ENTER或GEOFENCE_TRANSITION_EXIT已被触发到同一个GeoFence对象,GEOFENCE_TRANSITION_DWELL不会触发。