onReceive from BroadcastReceiver不叫
我有一个音乐应用程序,我试图在通知栏上添加一些操作按钮。onReceive from BroadcastReceiver不叫
我想是这样的:
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
我的清单文件包含reciever像这样:
<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没有零参数的构造函数
使用Google搜索一下,我发现我必须使用静态分类或者我必须在父类中注册/取消注册。
所以这是我做过什么:
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
又坏了。
回答:
其实为什么当你点击暂停您的广播reciever不叫的原因,以前和下一个按钮是因为,你已经设置了未决的意图解雇的胡亚蓉,相反,你必须的待定意图火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);;
在所有三个改正待决,你写意图码
UPDATE
,为什么你还没有接收到广播中的原因您的广播接收器是因为您正在将您的Receiver注册为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文件
或 programitically you c在代码
NotificationReciever mReciever = new NotificationReciever(); this.registerReceiver(
mReciever,new IntentFilter("PREVIOUS"));
this.registerReceiver(
mReciever,new IntentFilter("PAUSE"));
this.registerReceiver(
mReciever,new IntentFilter("NEXT"));
的注册,但如果你注册这个programitically,请确保您注销它,而服务是被破坏掉了。否则,您可能会泄露广播接收器对象
以上是 onReceive from BroadcastReceiver不叫 的全部内容, 来源链接: utcz.com/qa/262551.html