未调用 BroadcastReceiver 的 onReceive

onReceive from BroadcastReceiver is not called

我有一个音乐应用程序,我试图在其中的通知栏上添加一些操作按钮。

我试过这样的事情:

 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);

}

我在此服务中声明了一个 NotificationReciever class

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;
               }
           }
       }
    }
}

结构看起来像这样:

-MusicService extends Service 
    --NotificationReciever extends BroadcastReceiver

我的清单文件包含这样的接收器:

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

当我 运行 我的音乐播放时,通知确实出现了按钮,但它们似乎没有触发 onReceive 功能?

我在这里错过了什么?

更新:

跟着哈西夫说的答案,我好像发现了一个错误

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

谷歌搜索,我发现我必须使用静态 class 或者我必须在父 class.

中使用 register/unregister

所以这就是我所做的:

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);

现在我没有收到上述错误,但是 onReceive 不再工作了。

实际上,当您点击 pausepreviousnext[ 时,您的广播接收器未被调用的原因=50=] 按钮是因为,您已将待定意图设置为触发 acitivity,而您必须将待定意图设置为触发 boradcast

而不是这个代码片段

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

你必须像这样更正它

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

对您编写的所有三个未决意图代码进行更正

更新

您仍然没有在您的广播接收器中接收到广播的原因是您以编程方式将您的接收器注册为 LocalBroadCast

PendingIntent一起使用时,LocalBroadcast将不会收到广播

所以请删除此行

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"));

相反,您只需在 Manifest.xml 文件 Manifest.xml 中 注册 接收方

以编程方式,您可以在代码中注册为

NotificationReciever mReciever = new NotificationReciever();

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

但是如果您以编程方式注册它,请确保在服务被销毁时注销它。否则你可能会泄露 BroadcastReceiver 对象