Android Geofencing不会触发IntentService

时间:2017-08-19 18:03:45

标签: java android google-api-client geofencing android-geofence

我正在构建一个移动应用程序,我将能够在Android手机上测试地理围栏的工作。我有一个使用GoogleApiClient FusedLocationApi.requestLocationUpdates的应用程序。我正在获取我在MapFragment中在地图上显示的所有更新。然后,我尝试使用GeofencingClient添加地理围栏。

private void addGeofenceList() {
    mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
            .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void aVoid) {
                    Log.wtf(TAG, "addGeofences() - succesfully added");
                }
            })
            .addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.wtf(TAG, "addGeofences() - failed to add");
                }
            });
}

此代码已执行,我回复说它们已成功添加。但是当我在地理围栏应该触发的位置时,它不会调用IntentService。我还添加了IntentService:

private PendingIntent getGeofencePendingIntent() {
    if (mGeofencePendingIntent != null) {
        return mGeofencePendingIntent;
    }
    Intent intent = new Intent(this, GeofenceService.class);
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

我还创建了一个GeofencingRequest,我在输入时设置了初始触发器。

private GeofencingRequest getGeofencingRequest() {
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder()
            .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
            .addGeofences(mGeofenceList);
    return builder.build();
}

这就是我创建ArrayList<Geofence> mGeofenceList的地方,我在其中添加了所有地理围栏:

public void createGeofenceList() {
    GeofenceList geofences = new GeofenceList();
    ArrayList<GeofenceModel> geofenceList = (ArrayList) geofences.returnGeofences();
    for (GeofenceModel geofence : geofenceList) {
        mGeofenceList.add(new Geofence.Builder()
                .setRequestId(geofence.getREQ_ID())
                .setCircularRegion(
                        geofence.getLocation().latitude,
                        geofence.getLocation().longitude,
                        geofence.getRadius()
                )
                .setExpirationDuration(geofence.getExpire())
                .setTransitionTypes(geofence.getTransition())
                .build());
    }
    addGeofenceList();
}

我的整个项目都在GitHub。所有的帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

在您的清单中,替换您的服务声明

<service android:name="services.GeofenceService" />

到这个

<service android:name="services.GeofenceService" android:exported="true"/>

基本上,您应将服务标记为exported,以便Android操作系统可以调用您的IntentService