后台执行限制和前台服务被杀死

时间:2018-11-09 11:42:04

标签: android android-mediaplayer android-8.0-oreo android-mediasession

我正在尝试使用Google的Universal Music Player加PlayerNotificationManager的代码播放一些音乐,该代码不是原始代码的一部分(但属于ExoPlayer 2)。

我的服务通过startForeground(...)的回调正确完成了PlayerNotificationManager

    playerNotificationManager.setNotificationListener(object : PlayerNotificationManager.NotificationListener {
        override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
            startForeground(NOW_PLAYING_NOTIFICATION, notification)
        }

        override fun onNotificationCancelled(notificationId: Int) {
        }

    })

一切似乎都很好,直到我将应用程序的主要活动放回去并且服务在几秒钟之内被杀死:

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


使用正确启动服务

Util.startForegroundService(...)

那么什么可能是错的?

1 个答案:

答案 0 :(得分:0)

在回调onNotificationStarted(或新引入的onNotificationPosted)中代替使用

startForeground(NOW_PLAYING_NOTIFICATION, notification)

您必须使用提供的ID

startForeground(notificationId, notification)

,当您通过PlayerNotificationManager创建通知时,必须确保notificationId不是0

相关问题