繁体   English   中英

Android Geofencing不会触发IntentService

[英]Android Geofencing dosen't trigger IntentService

我正在构建一个移动应用程序,通过它可以测试Android手机上的地理围栏工作。 我有一个将GoogleApiClient用于FusedLocationApi.requestLocationUpdates的应用程序。 我正在MapFragment中获得所有显示在地图上的更新。 然后,我尝试使用GeofencingClient添加地理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上 所有帮助将不胜感激!

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

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

对此

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM