2017-12-18 183 views
1

我有一個音樂應用程序,我試圖在通知欄上添加一些操作按鈕。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又壞了。

回答

2

其實爲什麼當你點擊暫停您的廣播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

使用的PendingIntentLocalBroadcast將不會收到廣播

所以請

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,請確保您註銷它,而服務是被破壞掉了。否則,您可能會泄露廣播接收器對象

+0

'java.lang.RuntimeException:無法實例化接收器com.example.tilak.imusicplay.MusicService $ NotificationReciever:java.lang.InstantiationException:java.lang.Class 沒有零參數構造函數' 得到此錯誤?有任何想法嗎? –

+0

@BOTJr。這個問題是因爲你聲明BroadcastReciever是服務的內部類,當你聲明這是一個內部類時,你不能在manifest.xml文件中註冊該Reciever –

+0

是的,我必須使用靜態類或我必須註冊/註銷我的父母課堂中的收件人。我使用了它。但我不認爲這是太工作 –

相關問題