Context.startForegroundService()然后未调用Service.startForeground() [英] Context.startForegroundService() did not then call Service.startForeground()

查看:2088
本文介绍了Context.startForegroundService()然后未调用Service.startForeground()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android O OS上使用Service类.

I am using Service Class on the Android O OS.

我计划在后台使用Service.

Android文档指出

如果您的应用程序的目标是API级别26或更高级别,则除非应用程序本身位于前台,否则系统会限制使用或创建后台服务.如果应用程序需要创建前台服务,则该应用程序应调用startForegroundService().

如果使用startForegroundService(),则Service会引发以下错误.

If you use startForegroundService(), the Service throws the following error.

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

这是怎么了?

推荐答案

来自Google在 Android 8.0行为更改:

即使应用程序在后台,系统仍允许应用程序调用Context.startForegroundService().但是,应用程序必须在创建服务后的五秒钟内调用该服务的startForeground()方法.

The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.

解决方案: 为使用Context.startForegroundService()

另请参见: Android 8.0(奥利奥(Oreo))背景执行限制

这篇关于Context.startForegroundService()然后未调用Service.startForeground()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆