我有一個音樂應用程序,我試圖在通知欄上添加一些操作按鈕。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
又壞了。
'java.lang.RuntimeException:無法實例化接收器com.example.tilak.imusicplay.MusicService $ NotificationReciever:java.lang.InstantiationException:java.lang.Class沒有零參數構造函數' 得到此錯誤?有任何想法嗎? –
@BOTJr。這個問題是因爲你聲明BroadcastReciever是服務的內部類,當你聲明這是一個內部類時,你不能在manifest.xml文件中註冊該Reciever –
是的,我必須使用靜態類或我必須註冊/註銷我的父母課堂中的收件人。我使用了它。但我不認爲這是太工作 –