接收来自Android Oreo上的通知的广播

编程入门 行业动态 更新时间:2024-10-19 17:34:08
本文介绍了接收来自Android Oreo上的通知的广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在即时贴中有一个自定义按钮. 我曾经在其上附加PendingIntent来接收按钮的点击:

I have a custom button in a sticky notification. I used to attach a PendingIntent to it for receiving button clicks:

Intent intent = new Intent(); intent.setAction("com.example.app.intent.action.BUTTON_CLICK"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 2000, intent, PendingIntent.FLAG_UPDATE_CURRENT); contentViewExpanded.setOnClickPendingIntent(R.id.button, pendingIntent);

当我在Oreo上运行此代码时,我在logcat中得到BroadcastQueue: Background execution not allowed,并且没有收到按钮点击.

When i run this code on Oreo , i get BroadcastQueue: Background execution not allowed in logcat and don't receive button click.

我在清单中注册了接收者:

I registered receiver with manifest:

<receiver android:name=".BroadcastReceiver.NotificationActionReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="com.example.app.intent.action.BUTTON_CLICK"/> </intent-filter> </receiver>

我还尝试在我的代码中注册接收者:

I also tried registering receiver in my code:

NotificationActionReceiver mMyBroadcastReceiver = new NotificationActionReceiver(); IntentFilter filter = new IntentFilter("com.example.app.intent.action.BUTTON_CLICK"); mContext.registerReceiver(mMyBroadcastReceiver, filter);

这有效,但仅当用户可见该应用程序时.

This works but only when the app is visible to user.

感谢帮助

推荐答案

当显式Intent将起作用时,切勿使用隐式Intent.

Never use an implicit Intent when an explicit Intent will work.

替换:

Intent intent = new Intent(); intent.setAction("com.example.app.intent.action.BUTTON_CLICK");

具有:

Intent intent = new Intent(this, NotificationActionReceiver.class);

并从NotificationActionReceiver <receiver>元素中删除<intent-filter>.

更多推荐

接收来自Android Oreo上的通知的广播

本文发布于:2023-11-27 08:19:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通知   Android   Oreo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!