我有一个置顶通知中的自定义按钮。
我曾经附加了一个PendingIntent
以便接收按钮点击:
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
,并且无法接收按钮点击。
我在清单文件中注册了接收器:
<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>
我也尝试在我的代码中注册接收器:
NotificationActionReceiver mMyBroadcastReceiver = new NotificationActionReceiver();
IntentFilter filter = new IntentFilter("com.example.app.intent.action.BUTTON_CLICK");
mContext.registerReceiver(mMyBroadcastReceiver, filter);
这个方法有效,但仅限于应用程序对用户可见的情况下。
谢谢帮助。