我一直在尝试遵循 Android 通用音乐播放器示例来了解如何处理通知。
暂停音频播放会触发 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();
看起来该示例为这个确切的场景添加了这一行。但它似乎并没有删除“幽灵”通知。我看到调试消息“取消所有通知”
还有什么我应该寻找的吗?
更新:
忘记提及,重新出现的通知似乎是不可交互的。播放/跳过/等等。按钮什么也不做。当延迟停止处理程序运行并确定没有音频正在播放时,通知最终消失。
更新2:
Ian 建议我监视 onStartCommand Intent 。
@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 和 .notify() 的其他出现情况,但没有任何运气。日志消息描述了事件的确切顺序。
最佳答案
我可能已经想出了一个解决方案。
我刚刚找到了 onTaskRemove 方法。我想我可以用它来停止播放未运行的服务。
@Override
public void onTaskRemoved(Intent rootIntent)
{
if (getPlaybackState().getState() != PlaybackStateCompat.STATE_PLAYING) {
stopSelf();
}
}
如果这是一个坏主意,请告诉我。
关于Android 通知再次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38382108/