androidandroid-notificationsandroid-8.0-oreoforeground-service

Android O: Service.stopForeground equivalent


I'm currently trying to make some kind of music player app. For playback I created a separate service. Because I want to keep it playing all the time, I'm calling Service.startForeground(). While compiling for Android O (SDK level 27) I have to use Context.startForegroundService() due to Androids "new" background execution limits.

This works fine, but when the user pauses playback, I want the notification to be dismissable. In the past, I was able to call stopForeground(), but this has no effect on Android O. Is there any other way to switch between background and foreground service or any other chance to implement this behaviour? Apps like Google Play Music are using exactly this notification pattern, but how do they do it?


Solution

  • This happened because I used the wrong method. You have to use stopForeground(STOP_FOREGROUND_DETACH) instead of stopForeground(true).