使用BroadcastReceiver的Awareness API和Android O.

Mic*_*anz 16 android broadcastreceiver android-broadcastreceiver google-awareness android-8.0-oreo

我有一个Android应用程序,它使用Awareness API在插入耳机时设置围栏.

我使用代码实现了AwarenessFence,就像以下示例中所示:https://developers.google.com/awareness/android-api/fence-register.

我有一个PendingIntent定义为:

PendingIntent.getBroadcast(context, 0, new Intent("my.application.packageFENCE_RECEIVER_ACTION"), 0)
Run Code Online (Sandbox Code Playgroud)

然后在我的AndroidManifest.xml文件中

<receiver android:name=".fence.FenceDetector$MyFenceReceiver">
<intent-filter>
    <action android:name="my.application.packageFENCE_RECEIVER_ACTION" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

这是在Manifest中声明的,因为即使我的应用程序在后台,我也希望接收广播.

这在Android 7.0及更低版本上运行良好,但是当我在Android 8.0上运行时,我收到错误:

BroadcastQueue: Background execution not allowed: receiving Intent { act=my.application.packageFENCE_RECEIVER_ACTION
Run Code Online (Sandbox Code Playgroud)

我认为这是由于Android O上后台执行的新限制.

任何人都可以告诉我如何注册一个广播接收器,它可以在运行API 26的Android设备上在后台监听感知围栏触发器.

让我知道如果有些事情不清楚或者我需要详细说明.

提前致谢

Bud*_*ius 13

我现在无法在设备上测试它,但从我读过的所有内容来看,限制仅限于隐式广播.这意味着,如果您创建一个显式广播,那就是让它工作所需的一切.

这意味着代替:

// implicit intent matching action
PendingIntent.getBroadcast(context, 0,
    new Intent("my.application.packageFENCE_RECEIVER_ACTION"), 0)
Run Code Online (Sandbox Code Playgroud)

你做吧:

// explicit intent directly targeting your class
PendingIntent.getBroadcast(context, 0,
    new Intent(context, FenceDetector.MyFenceReceiver.class), 0)
Run Code Online (Sandbox Code Playgroud)


Roh*_*ark 6

我做了一点挖掘,偶然发现了CommonsWare的这篇博客文章.它说明了你所面临的问题.

从上面的帖子: -

对于具有足够高的targetSdkVersion的应用程序,Android O中更具争议性的变化之一是对隐式广播的有效禁止.

因此,根据这一点,我不认为您的问题与Awareness API有任何关系.相反,这是因为Android 8中引入了新的行为.

不幸的是,到目前为止,似乎没有可行的解决方案.同样,从同一篇文章中删除: -

如果您正在接收系统发送的隐式广播(例如,ACTION_PACKAGE_ADDED),请将targetSdkVersion保持在25或更低,直到我们找出更好的解决方法(希望)不涉及轮询.

所以,希望在不久的将来有更好的解决方案8.同时,您可以考虑其他选项或考虑降低targetSDK.