2016-07-14 53 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(); 
    } 
} 

如果这是一个坏主意,请让我知道。