我一直在尝试按照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();
看起来这个例子为这个确切的场景添加了这一行。但它似乎没有删除“幽灵”通知。我看到调试消息“取消所有通知”
还有什么我应该寻找的吗?
更新
忘记提及重新出现的通知似乎是不可互动的。播放/跳过/等。按钮什么都不做。当延迟停止处理程序运行并确定没有播放音频时,通知最终会消失。
更新2:
Ian建议我监控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和.notify()的其他内容,没有任何运气。日志消息描述了事件的确切顺序。
答案 0 :(得分:2)
我可能想出一个解决方案。
我刚刚找到了onTaskRemove方法。我想我可以用这个来停止服务播放没有运行。
@Override
public void onTaskRemoved(Intent rootIntent)
{
if (getPlaybackState().getState() != PlaybackStateCompat.STATE_PLAYING) {
stopSelf();
}
}
如果这是一个坏主意,请告诉我。