2016-07-14 61 views
2

我一直在嘗試關注如何處理通知的Android Universal Music Player示例。Android通知重新出現

暫停audioplayback觸發器stopForeground(false) 我使用false來保持通知。

然後,如果我從最近關閉應用程序通知消失,然後重新出現MediaBrowserCompat瀏覽器重新創建時。

我的通知管理器有這條線它它的初始值設定項。

// Cancel all notifications to handle the case where the Service was killed and 
     // restarted by the system. 
     Log.d(Constants.TAG, "Canceling all notifications"); 
     mNotificationManager.cancelAll(); 

看起來好像這個例子爲這個確切的場景添加了這一行。但它似乎並沒有刪除「幽靈」通知。我看到調試消息「取消所有通知」

還有什麼我應該找的?

更新:

忘了提,即重新出現的通知似乎是無交互。播放/跳過/等。按鈕什麼都不做。當延遲停止處理程序運行並且確定沒有音頻正在播放時,通知最終會消失。

Updaet 2:

伊恩建議我監視onStartCommand意圖。

@Override 
    public int onStartCommand(Intent startIntent, int flags, int startId) 
    { 

     Log.d("start command", "On Start called"); 
     Log.d("start command", "Start flag = " + flags); 
     Log.d("start command", "start Id = " + startId); 

     if (startIntent != null) 
     { 
      Log.d("start command", startIntent.toString()); 
      MediaButtonReceiver.handleIntent(mSession, startIntent); 
     } 
     // Reset the delay handler to enqueue a message to stop the service if 
     // nothing is playing. 
     mDelayedStopHandler.removeCallbacksAndMessages(null); 
     mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY); 
     return START_STICKY; 
    } 

這是日誌,我得到

07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: On Start called 
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: Start flag = 0 
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: start Id = 1 
07-15 15:00:29.313 18901-18901/com.hackmodford.bigfinish D/start command: Intent { cmp=com.hackmodford.bigfinish/.mediaPlayerService.MediaPlayerService } 
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: On Start called 
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: Start flag = 0 
07-15 15:00:38.743 19399-19399/com.hackmodford.bigfinish D/start command: start Id = 3 

前4行從開始播放。然後,我暫停播放並關閉最近的應用程序。通知消失,然後重新出現。然後接下來的3條線張貼在顯示器上。

更多更新:

我添加日誌每次.notify(),.startForeground和.stopForeground()被稱爲我的通知管理器。結果如下。

07-15 15:09:10.052 28167-28167/com.hackmodford.bigfinish D/start command: start foreground because start notification was called 
07-15 15:09:10.193 28167-28167/com.hackmodford.bigfinish D/start command: On Start called 
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: Start flag = 0 
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: start Id = 1 
07-15 15:09:10.194 28167-28167/com.hackmodford.bigfinish D/start command: Intent { cmp=com.hackmodford.bigfinish/.mediaPlayerService.MediaPlayerService } 
07-15 15:09:10.210 28167-28167/com.hackmodford.bigfinish D/start command: notify because metadata changed 
07-15 15:09:16.678 28167-28167/com.hackmodford.bigfinish D/start command: notify because state changed 
07-15 15:09:16.764 28167-28167/com.hackmodford.bigfinish D/start command: stopForeground(false) because state is paused 
07-15 15:09:16.891 28167-28167/com.hackmodford.bigfinish D/start command: notify because metadata changed 
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: On Start called 
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: Start flag = 0 
07-15 15:09:30.841 28803-28803/com.hackmodford.bigfinish D/start command: start Id = 3 

更新:我搜索我的整個項目startService,startForeground(),stopForeground其他OCCURENCES和.notify()沒有任何的運氣。日誌消息描述事件的確切順序。

+0

什麼是MediaBrowserCompat在您的用例中,以及爲什麼如果您從最近消除了您的活動,它會被重新創建? – JoxTraex

+0

我正在製作一個有聲讀物播放器,並遵循Google的通用音樂播放器示例。我相信它會被重新創建,因爲onStart返回START_STICKY – Hackmodford

+0

如果我使它START_NOT_STICKY我沒有問題。但我不確定這是否會導致其他問題... – Hackmodford

回答

2

我可能想出了一個解決方案。

我剛剛找到了onTaskRemove方法。我想我可以用這個來停止正在播放的服務沒有運行。

@Override 
public void onTaskRemoved(Intent rootIntent) 
{ 
    if (getPlaybackState().getState() != PlaybackStateCompat.STATE_PLAYING) { 
     stopSelf(); 
    } 
} 

如果這是一個壞主意,請讓我知道。