大佬教程收集整理的这篇文章主要介绍了当应用程序在Android 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的前台服务时,看起来很奇怪,并且当你的应用程序也在前台时,系统拒绝启动前台服务的应用程序.我还没有找到与此相关的大量文档,但这是记录在案的行为吗?您的应用程序是否至少有一种方法可以知道它是否受到后台限制,因此不会尝试在前台启动服务?
我的服务代码基本上如下所示.我们的目标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) } } }
更新:我认为问题的另一部分还在尝试使用Intents来启动和停止服务,特别是因为在某些情况下我的服务已启动然后停止太快.一个非常有用的thread with Ian Lake提供了有关服务的这些建议:
以上是大佬教程为你收集整理的当应用程序在Android Pie上受到后台限制时启动前台服务全部内容,希望文章能够帮你解决当应用程序在Android Pie上受到后台限制时启动前台服务所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。