I'm following the Android guide to use MediaBrowserServiceCompat in a media player app, but the service is destroyed on app exit.
<service android:name=".media.PlaybackService">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
On application start service is created correctly. On button pressed mediaController.transportControls.play()
is called and after this onPlay()
method from my session callback class is invoked. Inside onPlay()
service is started: context.startService(Intent(context, MediaBrowserServiceCompat::class.java))
(also tried startForegroundService
for Oreo and higher) and set to foreground: this@PlaybackService.startForeground(SERVICE_ID, notificationBuilder?.build())
. Notification channel for Oreo nad higher API is created.
For now notification is launched and I can see service is in foreground by issuing command:
$adb shell dumpsys activity services PlaybackService (...) isForeground=true foregroundId=1 foregroundNoti=Notification(channel=MyChannelId pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x62 color=0xff000000 category=transport actions=1 vis=PUBLIC) (...)
So far everything works fine but when I press home button I'm expecting the service remain started, but instead the onDestroy()
method of my MediaBrowserServiceCompat
class is invoked. After returning to the app and launching service once again it is not set to foreground (no notification as well).
I'm using the only one startService
call in my app and removed every call to stopSelf
and stopForeground
for testing.
On APIs 28, 26, 22 and 18 I'm getting the same result.
I've also tried returning START_STICKY
from onStartCommand()
.