ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

当应用程序在Android Pie上受到后台限制时启动前台服务

2019-07-05 13:23:00  阅读:555  来源: 互联网

标签:android foreground-service


使用Android 9.0(Pie),用户可以限制您的应用程序通过设置进行后台工作.当我们尝试在Pie设备上启动前台服务时,如果应用程序受到后台限制,即使应用程序活动完全位于前台,我们的应用程序也会遇到以下异常.

RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

我们在启动的服务中调用startForeground(),但尝试启动服务时的系统日志显示:

system_process W/ActivityManager: Service.startForeground() not allowed due to bg restriction

当你跟踪the documented steps的前台服务时,看起来很奇怪,并且当你的应用程序也在前台时,系统拒绝启动前台服务的应用程序.我还没有找到与此相关的大量文档,但这是记录在案的行为吗?您的应用程序是否至少有一种方法可以知道它是否受到后台限制,因此不会尝试在前台启动服务?

enter image description here

我的服务代码基本上如下所示.我们的目标api是27.

class MyService : Service() {

    override fun onCreate() {
        super.onCreate()
        // create notification
        startForeground(NOTIFICATION_ID, notification)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        if (intent == null || intent.action == null || intent.action == ACTION_STOP) {
            quit()
        }
        doWork()
        return START_NOT_STICKY
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onTaskRemoved(rootIntent: Intent?) {
        super.onTaskRemoved(rootIntent)
        quit()
    }

    private fun quit() {
        stopForeground(true)
        stopSelf()
    }

    companion object {

        fun start(context: Context) {
            val intent = Intent(context, MyService::class.java)
            intent.action = ACTION_START
            ContextCompat.startForegroundService(context, intent)
        }

        fun stop(context: Context) {
            val intent = Intent(context, MyService::class.java)
            intent.action = ACTION_STOP
            ContextCompat.startForegroundService(context, intent)
        }
    }
}

解决方法:

想知道崩溃实际上会发生在MyService.stop()中对ContextCompat.startForegroundService()的第二次调用,我用它来发送一个带有“停止”动作的Intent来停止服务而不是调用context.stopService( ).起初我虽然需要在服务中手动调用stopForeground(),但是调用context.stopService()似乎停止了前台服务并且仍然删除了通知并且不会导致崩溃,所以我决定重构我如何处理停止服务.

更新:我认为问题的另一部分还在尝试使用Intents来启动和停止服务,特别是因为在某些情况下我的服务已启动然后停止太快.一个非常有用的thread with Ian Lake提供了有关服务的这些建议:

I’d strongly suggest against using startService as a way to pass messages to your service. Using an EventBus or LocalBroadcastReceiver is a lot better way of passing messages without conflating that with lifecycle actions. I’d also avoid having external components call stopService() directly – let the service itself manage its own lifecycle, reacting to events you send its way.

标签:android,foreground-service
来源: https://codeday.me/bug/20190705/1387787.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有