Android 通知再次出现

Android Notification Reappears

我一直在尝试按照 Android Universal Music Player 示例来了解如何处理通知。

暂停音频播放会触发 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();

看起来该示例为这个确切的场景添加了这一行。但它似乎并没有删除 "ghost" 通知。我看到调试消息 "cancelling all notifications"

还有什么我应该寻找的吗?

更新:

忘记提及重新出现的通知似乎无法交互。 Play/Skip/etc。按钮什么都不做。当延迟停止处理程序运行并确定没有音频在播放时,通知最终消失。

更新 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 和 .notify() 的其他出现,但没有任何运气。日志消息描述了事件的确切顺序。

我可能想出了一个解决办法。

我刚找到 onTaskRemove 方法。我想我可以用这个来停止服务播放不是 运行。

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

如果这是个坏主意,请告诉我。