android - 如何在奥利奥中启动后台服务?

标签 android service android-8.0-oreo

oreo 版本之前,默认所有服务在后台使用 startService() 启动,但在 oreo 中启动服务有一些限制后台服务,我可以在 oreo 中使用 startService() 启动后台服务吗?

最佳答案

只要您的应用程序处于前台,您就可以使用 startService() ,如果您的应用程序进入后台并且您调用 startService() ,您将得到 IllegalStateException

或者你可以使用startForeground()来启动一个服务

来自文档

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

检查 Documentation了解更多信息

关于android - 如何在奥利奥中启动后台服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52552450/

相关文章:

android - ActionBar 进度微调器,同时将 1000 的标记添加到 android Googlemap

Android 8.0,startActivityForResult设置,如果用户单击左上角的后退按钮,则不会返回到我的应用程序

android - 当用户在 Android 中切换应用程序的 "Background restriction"时是否有系统广播?

java - 检查 Android 上特定设备上的蓝牙状态

java - RecyclerView 不显示 - Android

android - 如何在 mpandroidchart 中设置条形图的最大缩放级别?

java - 如何从 Activity 访问服务的变量

angular - Angular 4 中的并行 HTTP 请求

c# - 当作为 Windows 服务运行时,如何让程序自己截屏?

android - 为什么带有 HTML 选择标签的 Android OS 8 WebView 会使应用程序崩溃