I'm trying to play some music using code from Google's Universal Music Player plus PlayerNotificationManager, which is not a part of the original code (but belongs to ExoPlayer 2).
My service does startForeground(...) properly by a callback from PlayerNotificationManager:
playerNotificationManager.setNotificationListener(object : PlayerNotificationManager.NotificationListener {
override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
startForeground(NOW_PLAYING_NOTIFICATION, notification)
}
override fun onNotificationCancelled(notificationId: Int) {
}
})
And everything seems to be OK until I leave the main activity of my application with back and the service gets mercifully killed within seconds with:
2018-11-09 12:15:28.859 3680-3695/? W/ActivityManager: Stopping service due to app idle: u0a577 -1m19s332ms pl.qus.xenoamp2/pl.qus.xenoamp.xenoservice.MusicService
The service is started properly by using
Util.startForegroundService(...)
So what could be possibly wrong?
In the callback
onNotificationStarted(or newly introducedonNotificationPosted) instead of usingyou have to use the provided id
and when you create the notification by PlayerNotificationManager you have to make sure the
notificationIdis not0.