gpt4 book ai didi

android - Context.startForegroundService() 没有调用 Service.startForeground()

转载 作者:IT老高 更新时间:2023-10-28 12:53:59 42 4
gpt4 key购买 nike

我在 Android O 操作系统上使用 Service 类。

我打算在后台使用Service

Android documentation声明

If your app targets API level 26 or higher, the system imposes restrictions on using or creating background services unless the app itself is in the foreground. If an app needs to create a foreground service, the app should call startForegroundService().

如果使用startForegroundService()Service会抛出如下错误。

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

这是怎么回事?

最佳答案

来自 Google 的文档 Android 8.0 behavior changes :

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.

解决方案:为您使用的 ServiceonCreate() 中调用 startForeground() Context.startForegroundService()

另请参阅:Background Execution Limits适用于 Android 8.0(奥利奥)

关于android - Context.startForegroundService() 没有调用 Service.startForeground(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44425584/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com