简体   繁体   English

从通知接收Intent的Receiver问题

[英]Problem with Receiver which receives Intents from Notification

I have a code that send a broadcast to a broadcast receiver. 我有一个代码,用于向广播接收器发送广播。

Intent intentPrev = new Intent(ACTION_PREV);
        PendingIntent pendingIntentPrev = PendingIntent.getBroadcast(this, 0, intentPrev, PendingIntent.FLAG_UPDATE_CURRENT);
        LocalBroadcastManager.getInstance(this).sendBroadcast(intentPrev);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);

In another class I have a Receiver : 在另一个班级我有一个Receiver

private BroadcastReceiver NotificationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("PREVIOUS")){
                playPrev();
            }
        }
    };

And in onCreate method I register this receiver: onCreate方法中我注册了这个接收器:

LocalBroadcastManager.getInstance(this).registerReceiver(NotificationReceiver, new IntentFilter("PREVIOUS"));

The main aim was to reach the following result: when user clicks on button Previous in notification, the previous song will play. 主要目的是达到以下结果:当用户点击通知中的按钮Previous时,将播放上一首歌曲。 But when I run the app and choose the music I can't listen to music, as always the previous plays. 但是,当我运行应用程序并选择音乐时,我无法听音乐,就像以前的播放一样。 So, it seems that there's a perpetual loop somewhere. 所以,似乎某处有一个永久的循环。 What's the matter? 怎么了? How to solve this problem if I want to play only one previous song but not all previous songs? 如果我只想播放以前的一首歌而不是之前的所有歌曲,如何解决这个问题?

There are two types of broadcasts: system broadcasts and local broadcasts. 有两种类型的广播:系统广播和本地广播。

Local broadcasts work through LocalBroadcastManager exclusively . 本地广播通过LocalBroadcastManager工作。 If you see anything else tied to "broadcast", 99.99% of the time, that is referring to system broadcasts. 如果您看到与“广播”相关的任何其他内容,99.99%的时间是指系统广播。

In particular, PendingIntent.getBroadcast() gives you a PendingIntent that will send a system broadcast. 特别是, PendingIntent.getBroadcast()为您提供了一个发送系统广播的PendingIntent That, in turn, means that your receiver needs to be set up to receive a system broadcast, either because: 反过来,这意味着您的接收器需要设置为接收系统广播,因为:

  • It is registered in the manifest with a <receiver> element, or 它在清单中注册了<receiver>元素,或
  • It is registered dynamically by calling registerReceiver() on a Context (not on LocalBroadcastManager ) 通过在Context (而不是在LocalBroadcastManager )调用registerReceiver()来动态registerReceiver()

Note that on Android 8.0+, implicit broadcasts (ones with just an action string) are banned, in effect. 请注意,在Android 8.0+上,隐式广播(仅包含操作字符串的广告)实际上是被禁止的。 If you elect to register your receiver in the manifest, use an Intent that identifies the specific receiver (eg, new Intent(this, MyReceiverClass.class) ). 如果您选择在清单中注册接收方,请使用标识特定接收方的Intent (例如, new Intent(this, MyReceiverClass.class) )。 If you elect to register your receiver via registerReceiver() ... I think there is a recipe for how to handle that, but I forget the details. 如果您选择通过registerReceiver()注册您的接收器...我认为有一个如何处理它的方法,但我忘记了细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM