不调用来自BroadcastReceiver的onReceive

时间:2017-12-18 10:34:44

标签: java android broadcastreceiver action

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

我试过这样的事情:

 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

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功能?

我在这里缺少什么?

更新

跟着hasif说回答,我似乎发现了一个错误

  

java.lang.RuntimeException:无法实例化接收器com.example.tilak.imusicplay.MusicService $ NotificationReciev er:java.lang.InstantiationException:java.lang.Class没有零参数构造函数

谷歌搜索它,我发现我必须使用静态类,或者我必须在父类中注册/取消注册。

所以这就是我所做的:

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无效。

1 个答案:

答案 0 :(得分:5)

实际上当您点击暂停上一个下一个按钮时未调用广播接收器的原因是因为您已设置触发活动的未决意图,而您必须设置挂起意图以触发 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文件中的接收器

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

NotificationReciever mReciever = new NotificationReciever();

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

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