当应用程序在Oreo上处于前台时启动服务时发生IllegalStateException

时间:2018-09-28 06:28:56

标签: android service android-8.0-oreo

给出:我有一项服务,该服务从某项活动的onStart()开始,并且在同一活动的onStop()停止该服务。

已知::根据Android 8中的Android文档,存在后台限制,其中当应用程序在后台运行时,服务无法启动,并且会抛出IllegalStateException。 (https://developer.android.com/about/versions/oreo/background

推论:但是,此限制不会影响我的情况,因为我的服务仅在应用程序处于前台(onStart()onStop()之间)时运行。

问题:但是,我看到Google控制台发生一次崩溃,在Android 8(Oreo)设备上为行IllegalStateException投掷了startService()。知道为什么应用程序在前台启动服务时会引发此异常吗?将IllegalStateException投向startService()

时,还会有其他情况吗?

1 个答案:

答案 0 :(得分:-1)

尝试一下

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
      startforegroundservice(your_service);
}else{
      startservice(your_service);
}
相关问题