显式意图与PendingIntent。未收到BroadcastReceiver

时间:2018-10-31 09:30:58

标签: android android-intent android-pendingintent

我在运行时在应用程序onCreate()中注册了广播接收器:

context.registerReceiver(muteButtonListener, new IntentFilter());

然后在代码中的某处构造待处理的意图,以在通知中显示静音按钮:

Intent muteIntent = new Intent(context, MuteButtonListener.class);
PendingIntent pMuteIntent = PendingIntent.getBroadcast(context, REQUEST_MUTE_CALL, muteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

但是,当我单击静音按钮muteButtonListener时,不会接收广播。

然后我尝试使用IntentFilter添加一个操作,该操作也没有帮助:

Intent muteIntent = new Intent(context, MuteButtonListener.class);
intent.setAction("action");

最后,我删除了对具体组件的调用,并再次添加了动作:

Intent muteIntent = new Intent("action);

并使用此过滤器注册接收者:

context.registerReceiver(muteButtonListener, new IntentFilter("action"))

显然可以。

问题是为什么前两个选项不起作用?

0 个答案:

没有答案