android - 使用 BroadcastReceiver 的 Awareness API 和 Android O

标签 android broadcastreceiver android-broadcastreceiver android-8.0-oreo google-awareness

我有一个 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/

相关文章:

android - 在 Android 中使用 ScrollView 的图表

android - 如何使用 picasso 模糊图像?

android - 当 Http Post 成功完成并等待响应时互联网连接丢失怎么办?

android - 从代码注册广播接收器以在远程进程中运行

android - 如何检查应用程序是否在 BroadcastReceiver 的前台后台运行

java - 我的php脚本已连接,但android studio不会使用它。没有可见的错误输出

android - 我自己的应用程序之间的安全通信

java - 是否可以通过广播接收器在 Android 应用程序之间传递自定义对象?

android - 警报管理器在特定的给定时间间隔内不工作

android - 如何在 10 秒后取消警报?