I have been trying to follow the Android Universal Music Player example for how to handle notifications.
Pausing audioplayback triggers stopForeground(false) I use false to keep the notification around.
Then if I dismiss the app from recents the notification disappears and then reappears when the MediaBrowserCompat browser gets recreated.
My notification manager has this line it its initializer.
// 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();
It looks like the example added this line for this exact scenario. But it doesn't seem to remove the "ghost" notification. I see the debug message "cancelling all notifications"
Is there anything else I should look for?
Update:
Forgot to mention that the notification that re-appears seems to be non-interactable. Play/Skip/etc. buttons do nothing. The notification eventually disappears when the delayed stop handler runs and determines that no audio is playing.
Updaet 2:
Ian suggested that I monitor the 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;
}
This is the log that I get
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
First 4 lines are from starting playback. I then pause playback and dismiss the app from the recent. The notification goes away, then reappears. Then the next 3 lines get posted in the monitor.
More Updates:
I added logs each time .notify(), .startForeground, and .stopForeground() were called in my notification manager. Here are the results.
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
Update: I've searched my entire project for other occurences of startService, startForeground(), stopForeground, and .notify() without any luck. The log messages describe the exact sequence of event.