我在 Android O 操作系统上使用 Service
类。
我打算在后台使用 Service
。
Android 文档 指出
如果您的应用程序以 API 级别 26 或更高级别为目标,系统会限制使用或创建后台服务,除非应用程序本身位于前台。如果应用需要创建前台服务,应用应该调用
startForegroundService()
。
如果您使用 startForegroundService()
,则 Service
会引发以下错误。
Context.startForegroundService() did not then call
Service.startForeground()
这有什么问题?
原文由 NiceGuy 发布,翻译遵循 CC BY-SA 4.0 许可协议
就我而言,我在服务有机会在内部调用 startForeground 之前调用了 context.stopService(outside service)。