java - Android 地理围栏不触发 IntentService

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

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

最佳答案

在您的 list 中替换您的服务声明

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

到此

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

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

关于java - Android 地理围栏不触发 IntentService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774803/

相关文章:

java - FusedLocationApi 存在问题,已弃用

python - 在 Mac OS X 中使用 Python 自动授权 Google Drive

串行服务客户端的 Java 套接字服务器

android - FB 光刻 : How to set custom font for Text

java - Micronaut - Springframework @Bean 等效项是什么?

android - Html.fromhtml 的 Textview 占用了不需要的空间

android - react 原生构建 : Error while merging dex archives

oauth-2.0 - 为生产系统恢复已删除的 (?) Google OAuth 客户端应用程序

java - 放入内容:encoded in RSS feed using ROME

java - 如何动态更改 JFrame 内的 JPanel?