如何在Oreo中启动后台服务? [英] how to start a background service in oreo?
问题描述
默认情况下,所有服务都在oreo
之前的版本中使用startService()
在后台启动,但是在oreo
中有一些在后台启动服务的限制,我可以在oreo using
startService()
中启动后台服务吗? ?
by default all the service starts in Background using startService()
before oreo
version , but in oreo
there is some restriction to start a service in background, Can I start a background service in oreo using
startService()
?
推荐答案
只要您的应用程序位于前台,就可以使用startService(),如果您的应用程序位于后台并且调用了startService()
,则您将获得IllegalStateException
You can use startService() as long as your app is in foreground , if your app goes background and you call startService()
you will get IllegalStateException
或者,您可以使用startForeground()
来启动服务
Alternatively you can use startForeground()
to start a service
从文档
应用程序位于前台时,它可以自由创建和运行前台和后台服务.当应用进入后台时,它会有几分钟的窗口,仍然允许在其中创建和使用服务.在该窗口的末尾,该应用程序被认为是空闲的.此时,系统将停止应用程序的后台服务,就像该应用程序已调用服务的Service.stopSelf()方法一样
While an app is in the foreground, it can create and run both foreground and background services freely. When an app goes into the background, it has a window of several minutes in which it is still allowed to create and use services. At the end of that window, the app is considered to be idle. At this time, the system stops the app's background services, just as if the app had called the services' Service.stopSelf() methods
文档以获取更多信息
这篇关于如何在Oreo中启动后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!