使用Android 26接收地理围栏广播

使用Android 26接收地理围栏广播,android,broadcastreceiver,android-geofence,Android,Broadcastreceiver,Android Geofence,我正在注册接收geofence广播,它在SDK 25中运行良好。但在SDK 27中,这一点现在被打破了 我知道我无法通过清单文件注册以接收隐式广播,但我正在注册一个类特定广播,如下所示: Intent geofencingIntent = new Intent("GEOFENCE_BROADCAST"); geofencingIntent.putExtra("ID", geofenceId); geofencingIntent.setClass(context, GeofenceBroadcas

我正在注册接收geofence广播,它在SDK 25中运行良好。但在SDK 27中,这一点现在被打破了

我知道我无法通过清单文件注册以接收隐式广播,但我正在注册一个类特定广播,如下所示:

Intent geofencingIntent = new Intent("GEOFENCE_BROADCAST");
geofencingIntent.putExtra("ID", geofenceId);
geofencingIntent.setClass(context, GeofenceBroadcastReceiver.class);

PendingIntent geofencingPendingIntent = PendingIntent.getBroadcast(context, 1, geofencingIntent, PendingIntent.FLAG_UPDATE_CURRENT);

final Task<Void> geofenceTask = mGeofencingClient.addGeofences(geofencingRequest, geofencingPendingIntent);
Intent geofencingIntent=新意图(“地理围栏广播”);
geofencingIntent.putExtra(“ID”,geofencingentent.putExtra”);
setClass(上下文,GeofenceBroadcastReceiver.class);
PendingEvent GeoFencingPendingEvent=PendingEvent.getBroadcast(上下文,1,GeoFencingInput,PendingEvent.FLAG_UPDATE_CURRENT);
最终任务geofenceTask=mGeofencingClient.addGeofences(geofencingRequest,geofencingpendingcontent);
然而,我没有收到这个广播

关于为什么不接收这种“明确的”广播以及解决方法的任何建议。即使应用程序未运行或未激活,也必须接收广播

此外,我无法理解删除此功能背后的逻辑。是的,这是为了通过防止应用程序不必要地唤醒来节省电池。但是,在应用程序不运行时能够接收广播对我来说似乎是一个相当重要的功能。而鼓励作业调度(只能设置为15分钟的最小间隔)似乎有相当大的限制

我已经检查了以下答案,不认为这是重复的:


谢谢。

首先,如果您尚未在运行时注册braodcast,请注册它。 我会帮你的

如果在应用程序上下文中注册广播,则会收到 只要应用程序正在运行,就会广播

如果它仍然失败,那么通过onFailedListener检查异常

mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent())
        .addOnSuccessListener(this, new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                // Geofences added
                // ...
            }
        })
        .addOnFailureListener(this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // Failed to add geofences
                // ...
            }
        });
此外,我无法理解删除此功能背后的逻辑

这在

(代码)中有很好的描述,但是,我没有收到这个广播

关于为什么不接收这种“明确的”广播以及解决方法的任何建议。即使应用程序未运行或未激活,也必须接收广播


此外,我无法理解删除此功能背后的逻辑。是的,这是为了通过防止应用程序不必要地唤醒来节省电池。但是,在应用程序不运行时能够接收广播对我来说似乎是一个相当重要的功能。而鼓励作业调度(最小间隔只能设置为15分钟)似乎有相当大的限制性

我问了一个类似的问题,但还没有答案
private PendingIntent getGeofencePendingIntent() {
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
        // calling addGeofences() and removeGeofences().
        mGeofencePendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.
                FLAG_UPDATE_CURRENT);
        return mGeofencePendingIntent;
    }