简体   繁体   English

不调用来自BroadcastReceiver的onReceive

[英]onReceive from BroadcastReceiver is not called

I have a music application in which I am trying to add some action button on the notification bar. 我有一个音乐应用程序,我试图在通知栏上添加一些操作按钮。

I tried something like this: 我试过这样的事情:

 public void onPrepared(MediaPlayer mediaPlayer) {
    mediaPlayer.start();
    Intent onPreparedIntent=new Intent("MEDIA_PLAYER_PREPARED").putExtra("CURR_SONG",songposn);
    LocalBroadcastManager.getInstance(this).sendBroadcast(onPreparedIntent);
    Intent notintent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Notification.Builder builder=new Notification.Builder(this);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notintent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent prevPendingIntent=PendingIntent.getActivity
            (this,1,new Intent().setAction("PREVIOUS"),PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pausePendingIntent=PendingIntent.getActivity
            (this,2,new Intent().setAction("PAUSE"),PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent nextPendingIntent=PendingIntent.getActivity
            (this,3,new Intent().setAction("NEXT"),PendingIntent.FLAG_UPDATE_CURRENT);;
    builder.setContentIntent(pendingIntent).setSmallIcon(R.drawable.playicon)
            .addAction(R.drawable.back, "Previous", prevPendingIntent)
            .addAction(R.drawable.playsmall, "Pause", pausePendingIntent)
            .addAction(R.drawable.forw, "Next", nextPendingIntent)
            .setTicker(songArtist)
            .setOngoing(true).setContentTitle(songTitle).setContentText(songArtist);

    Notification not=builder.build();
    startForeground(MusicService.NOTIFY_ID,not);

}

I declared a NotificationReciever class inside this service 我在这个服务中声明了一个NotificationReciever

public class NotificationReciever extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("here","here");
       String action=intent.getAction();
       if(action!=null){
           switch (action){
               case "PREVIOUS":{
                   playPrev();
                   break;
               }
               case "PAUSE":{
                   pausePlayer();
                   LocalBroadcastManager.getInstance(MusicService.this).sendBroadcast(new Intent("STOP_THREAD"));
                   break;
               }
               case "NEXT":{
                   playNext();
                   break;
               }
           }
       }
    }
}

Structure looks something like this: 结构看起来像这样:

-MusicService extends Service 
    --NotificationReciever extends BroadcastReceiver

My manifest file contains reciever like this: 我的清单文件包含这样的收件人:

 <receiver  android:name=".MusicService$NotificationReciever">
        <intent-filter>
            <action android:name="PREVIOUS"/>
            <action android:name="PAUSE"/>
            <action android:name="NEXT"/>
        </intent-filter>
    </receiver>

When I run my music play, notification does come up with buttons but they don't seem to fire the onReceive function? 当我运行我的音乐播放时,通知会出现按钮,但它们似乎没有触发onReceive功能?

What am I missing here? 我在这里错过了什么?

Update: 更新:

Followed hasif sayed answer and I seem to found an error 跟着hasif说回答,我似乎发现了一个错误

java.lang.RuntimeException: Unable to instantiate receiver com.example.tilak.imusicplay.MusicService$NotificationReciev‌​er: java.lang.InstantiationException:java.lang.Class has no zero argument constructor java.lang.RuntimeException:无法实例化接收器com.example.tilak.imusicplay.MusicService $ NotificationReciev er:java.lang.InstantiationException:java.lang.Class没有零参数构造函数

Googling about it, I found that I have to use a static class or I have to register/unregister in the parent class. 谷歌搜索它,我发现我必须使用静态类或我必须在父类中注册/取消注册。

So this is what I did: 所以这就是我做的:

public void onCreate() {
        super.onCreate();
        //
        LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("PREVIOUS"));
        LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("PAUSE"));
        LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("NEXT"));
    }

PendingIntent prevPendingIntent=PendingIntent.getBroadcast
                (this,1,new Intent().setAction("PREVIOUS"),PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pausePendingIntent=PendingIntent.getBroadcast
                (this,2,new Intent().setAction("PAUSE"),PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent nextPendingIntent=PendingIntent.getBroadcast
                (this,3,new Intent().setAction("NEXT"),PendingIntent.FLAG_UPDATE_CURRENT);

Now I don't get this above error but onReceive is not working again. 现在我没有得到上面这个错误,但是onReceive再也没有了。

Actually the reason why your broadcast reciever is not called when you click on pause , previous and next button is because ,you have set the pending intent to fire an acitivity ,instead you have to set the pending intent to fire a boradcast 实际上,当您单击暂停上一个下一个按钮时未调用广播接收器的原因是,您已将挂起的意图设置为触发一个活动 ,而您必须设置挂起的意图以触发一个boradcast

instead of this code snippet 而不是这段代码

PendingIntent nextPendingIntent=PendingIntent.getActivity
            (this,3,new Intent().setAction("NEXT"),PendingIntent.FLAG_UPDATE_CURRENT);;

you have to correct it like this 你必须像这样纠正它

PendingIntent nextPendingIntent=PendingIntent.getBroadcast
            (this,3,new Intent().setAction("NEXT"),PendingIntent.FLAG_UPDATE_CURRENT);;

make corrections in all the three pending intent code which you have written 在您编写的所有三个待处理的意图代码中进行更正

UPDATE UPDATE

The reason why you still not receiving the broadcast in your Broadcast Receiver is because you are programitically registering your Receiver as LocalBroadCast 您仍然没有在广播接收器中接收广播的原因是因为您正在将接收器注册为LocalBroadCast

When using with PendingIntent , LocalBroadcast will not receive the Broadcast PendingIntent 一起使用时, LocalBroadcast将不会收到广播

so please remove this Line 所以请删除此行

LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("PREVIOUS"));
        LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("PAUSE"));
        LocalBroadcastManager.getInstance(this).registerReceiver(
                new NotificationReciever(),new IntentFilter("NEXT"));

Instead, you only have to register the receiver in the Manifest.xml file 相反,您只需在Manifest.xml文件中注册 接收器

or programitically you can register in code as 或者您可以在代码中注册为

NotificationReciever mReciever = new NotificationReciever();

this.registerReceiver(
                mReciever,new IntentFilter("PREVIOUS"));
this.registerReceiver(
                mReciever,new IntentFilter("PAUSE"));
this.registerReceiver(
                mReciever,new IntentFilter("NEXT"));

but if you register this programitically, make sure you unregister it while service is getting destroyed. 但如果您以编程方式注册,请确保在服务被销毁时取消注册 Otherwise you may LEAK the BroadcastReceiver Object 否则,您可能会泄漏BroadcastReceiver对象

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

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