2

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.

Hackmodford
  • 3,901
  • 4
  • 35
  • 78
  • What is MediaBrowserCompat in your use case and why does it get re-created if you destroyed your activity from recents? – JoxTraex Jul 14 '16 at 21:46
  • I am making an audiobook player and following the universal music player example by Google. I believe it gets recreated because onStart returns START_STICKY – Hackmodford Jul 15 '16 at 03:13
  • If I make it START_NOT_STICKY I don't have the problem. But I'm not sure if that might cause other issues... – Hackmodford Jul 15 '16 at 03:52
  • START_NOT_STICKY seems to have fixed the issue in 6.0.1 but I get the ghost notification is 5.1.1 :( – Hackmodford Jul 15 '16 at 16:16
  • Can you include a picture of this 'ghost notification'? – ianhanniballake Jul 15 '16 at 22:01
  • I'll do one better and provide a video. But it will take a while since my internet sucks. – Hackmodford Jul 15 '16 at 22:37
  • I think I've traced this down to doing too much in my Applications onCreate method. I noticed the the ghost notification disappears as soon as onCreate has finished. – Hackmodford Jul 16 '16 at 18:33

1 Answers1

2

I may have come up with a solution.

I just found the onTaskRemove method. I think I can use this to stop the service is playback is not running.

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

If this is a bad idea, please let me know.

Hackmodford
  • 3,901
  • 4
  • 35
  • 78