首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >后台执行限制和前台服务被杀

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

Stack Overflow用户
提问于 2018-11-09 19:42:04
回答 1查看 607关注 0票数 0

我正在尝试使用谷歌的通用音乐播放器加上PlayerNotificationManager的代码来播放一些音乐,这不是原始代码的一部分(但属于ExoPlayer 2)。

我的服务通过来自PlayerNotificationManager的回调正确地执行startForeground(...)

代码语言:javascript
复制
    playerNotificationManager.setNotificationListener(object : PlayerNotificationManager.NotificationListener {
        override fun onNotificationStarted(notificationId: Int, notification: Notification?) {
            startForeground(NOW_PLAYING_NOTIFICATION, notification)
        }

        override fun onNotificationCancelled(notificationId: Int) {
        }

    })

一切似乎都很正常,直到我将应用程序的主要活动留给back,服务在几秒钟内被仁慈地杀死,如下所示:

代码语言:javascript
复制
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(...)

那么,可能出了什么问题呢?

EN

回答 1

Stack Overflow用户

发布于 2019-11-26 01:12:10

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

代码语言:javascript
复制
startForeground(NOW_PLAYING_NOTIFICATION, notification)

您必须使用提供的id

代码语言:javascript
复制
startForeground(notificationId, notification)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53225105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档