接收来自Android Oreo上的通知的广播 [英] Receiving broadcast from notification on 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上的通知的广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!