我有一个 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)
然后在我的 AndroidManifest.xml 文件中
<receiver android:name=".fence.FenceDetector$MyFenceReceiver">
<intent-filter>
<action android:name="my.application.packageFENCE_RECEIVER_ACTION" />
</intent-filter>
这是在 Manifest 中声明的,因为即使我的应用在后台,我也想接收广播。
这一切在 Android 7.0 及更低版本上运行良好,但是当我在 Android 8.0 上运行时出现错误:
BroadcastQueue: Background execution not allowed: receiving Intent { act=my.application.packageFENCE_RECEIVER_ACTION
我认为这是由于 Android O 上对后台执行的新限制。
谁能告诉我如何注册一个广播接收器,它可以在运行 API 26 的 Android 设备上在后台监听感知围栏触发器。
如果有不清楚的地方或者我需要详细说明的地方,请告诉我。
提前致谢
最佳答案
我现在无法在设备上对其进行测试,但据我所知,限制仅限于隐式广播。这意味着,如果您改为创建显式广播,那么您只需要这样做即可。
这意味着不是这个:
// implicit intent matching action
PendingIntent.getBroadcast(context, 0,
new Intent("my.application.packageFENCE_RECEIVER_ACTION"), 0)
你这样做:
// explicit intent directly targeting your class
PendingIntent.getBroadcast(context, 0,
new Intent(context, FenceDetector.MyFenceReceiver.class), 0)
关于android - 使用 BroadcastReceiver 的 Awareness API 和 Android O,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221057/