Android Google GeofencingClient未发送后续事件

时间:2017-11-07 20:12:20

标签: android location geofencing

我试图在Android上使用Google GeofencingClient(替换GeofencingApi)。在设置并添加地理围栏后,我确实从成功侦听器获得回调,并在我的IntentService中接收初始地理围栏输入/退出事件。但是,当我进出该区域时,我没有收到任何后续的地理围栏事件。我的半径设置为200米。这是我用来添加地理围栏的代码:

Geofence.Builder geoBuilder = new Geofence.Builder();
    geoBuilder.setRequestId(Constants.GEOFENCE_ID);
    geoBuilder.setNotificationResponsiveness(5 * 60 * 1000);
    geoBuilder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT);
    geoBuilder.setCircularRegion(latitude, longitude, 200f);
    geoBuilder.setExpirationDuration(Geofence.NEVER_EXPIRE);

    GeofencingRequest.Builder reqBuilder = new GeofencingRequest.Builder();
    reqBuilder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER | GeofencingRequest.INITIAL_TRIGGER_EXIT);
    reqBuilder.addGeofence(geoBuilder.build());

    PendingIntent pi = PendingIntent.getService(context, 0,
            new Intent(context, GService.class), PendingIntent.FLAG_UPDATE_CURRENT);

    GeofencingClient client = LocationServices.getGeofencingClient(context);
    Task task = client.addGeofences(reqBuilder.build(), pi);
    task.addOnSuccessListener(new OnSuccessListener() {
        @Override
        public void onSuccess(Object o) {
            Toast.makeText(context, "SUCCESS", Toast.LENGTH_SHORT).show();
        }
    });
    task.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Toast.makeText(context, "FAIL", Toast.LENGTH_SHORT).show();
        }
    });

在我的IntentService中,当geofencingEvent.hasError()返回true时,我会记录一个条目。但是我没有看到任何错误日志条目,因此在初始事件之后根本没有向服务发布地理围栏事件。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我遇到了相反的问题,在从LocationServices.GeofencingApi.addGeofences切换到geofencingClient.addGeofences之后,当我将地理围栏放在当前位置附近时,我不再获得初始“输入”意图。

之前(打印“成功”并解雇意图):

        LocationServices.GeofencingApi.addGeofences(
                googleApiClient,
                request,
                createGeofencePendingIntent()
        ).setResultCallback(status -> {
            Log.i(TAG, "onResult: " + status);
            if (status.isSuccess()) {
                mapsActivity.message("Adding geo-fence... Success");
                drawGeofence(position, radius);
            } else {
                mapsActivity.message("Adding geo-fence... FAILED!");
            }
        });

之后(打印“成功”但不会触发初始“输入”):

        geofencingClient.addGeofences(request, createGeofencePendingIntent())
                .addOnSuccessListener((Void)->{
                    mapsActivity.message("Adding geo-fence... Success");
                    drawGeofence(position, radius);
                }).addOnFailureListener((Void)->{
                    // TODO: inform about fail
                    mapsActivity.message("Adding geo-fence... FAILED!");
                })
        ;

更新:写完之后,它才刚刚开始工作(没有代码更改)。唯一的区别是同时应用程序“Google”更新到版本7.18.50.21.arm64