首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Geofence驻留时间未触发

Android Geofence驻留时间未触发
EN

Stack Overflow用户
提问于 2015-08-10 18:20:19
回答 3查看 3.9K关注 0票数 0

我最近更新了Google-Play-Service库,因为一些地理围栏和GCM所需的类已被删除/弃用。我已经从Creating and Monitoring Geofences下载了地理栅栏的示例代码

示例代码只监视入口和出口,我所做的是在我的IntentService中添加Geofence.GEOFENCE_TRANSITION_DWELL并设置游荡时间,我提供了逻辑来处理

代码语言:javascript
复制
geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL

进入/退出时的Geofence工作正常,但Dwell_TIME未触发。我已将geofences设置为

代码语言:javascript
复制
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());

我的意图服务为

代码语言:javascript
复制
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也从未触发过。请帮帮我。提前谢谢你

EN

回答 3

Stack Overflow用户

发布于 2017-01-01 15:16:51

仅当您进入地理围栏并在离开地理围栏之前使用setLoiteringDelay在那里停留设置的时间时,才会触发GEOFENCE_TRANSITION_DWELL。

另一点是,您必须将初始触发选项指定为GeofencingRequest.INITIAL_TRIGGER_DWELL和GeofencingRequest.INITIAL_TRIGGER_ENTER。检查下面的代码:

代码语言:javascript
复制
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) {

    }
票数 3
EN

Stack Overflow用户

发布于 2016-03-29 22:40:13

你能分享一下你创建geofence请求的代码吗?您还必须将初始触发器设置为驻留,如下所示:

代码语言:javascript
复制
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()接口的一个参数。例如:

代码语言:javascript
复制
LocationServices.GeofencingApi.addGeofences(
                     mGoogleApiClient,
                     getGeofencingRequest(),
                     getGeofencePendingIntent()
             );
票数 1
EN

Stack Overflow用户

发布于 2016-02-05 22:11:01

我怀疑如果GEOFENCE_TRANSITION_ENTER或GEOFENCE_TRANSITION_EXIT已经被触发到相同的GeoFence对象,则GEOFENCE_TRANSITION_DWELL不会触发。

票数 -3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31917062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档