我有一个音乐应用程序,我试图在通知栏上添加一些 *** 作按钮.
我试过这样的事情:
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: Unable to instantiate receiver com.example.tilak.imusicplay.MusicService$NotificationRecIEver: java.lang.InstantiationException:java.lang.class has no zero argument constructor
谷歌搜索它,我发现我必须使用静态类或我必须在父类中注册/取消注册.
所以这就是我做的:
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再也没有了.
@R_404_6120@:
实际上,当您单击暂停,上一个和下一个按钮时未调用广播接收器的原因是,您已将挂起的意图设置为触发一个活动,而您必须设置挂起的意图以触发一个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
您仍然没有在广播接收器中接收广播的原因是因为您正在将接收器注册为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你可以在代码注册
NotificationRecIEver mRecIEver = new NotificationRecIEver();this.registerReceiver( mRecIEver,new IntentFilter("PREVIoUS"));this.registerReceiver( mRecIEver,new IntentFilter("PAUSE"));this.registerReceiver( mRecIEver,new IntentFilter("NEXT"));
但如果您以编程方式注册,请确保在服务被销毁时取消注册.否则,您可能会泄漏broadcastReceiver对象
总结以上是内存溢出为你收集整理的java – 来自BroadcastReceiver的onReceive未被调用全部内容,希望文章能够帮你解决java – 来自BroadcastReceiver的onReceive未被调用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)