java.lang.IllegalStateException:不允许启动服务意图(来自 Activity onCreate)

tok*_*rio 2 android illegalstateexception android-intentservice android-8.0-oreo

I'm starting an IntentService from MainActivity:onCreate and I noticed this crash from the crash reporting only on Android Oreo above:

java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.company.mobile/com.company.mobile.gcm.RegistrationIntentService }: app is in background uid UidRecord{c96fbae u0a184 TPSL idle procs:1 seq(0,0,0)}
    at android.app.ContextImpl.startServiceCommon()(ContextImpl.java:1577)
    at android.app.ContextImpl.startService()(ContextImpl.java:1532)
    at android.content.ContextWrapper.startService()(ContextWrapper.java:664)
    at cs.a()(FirebaseUtility.java:42)
    at com.company.mobile.MainActivity.onCreate()(MainActivity.java:81)
    at android.app.Activity.performCreate()(Activity.java:7136)
    at android.app.Activity.performCreate()(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate()(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity()(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity()(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute()(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks()(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute()(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage()(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage()(Handler.java:106)
    at android.os.Looper.loop()(Looper.java:193)
    at android.app.ActivityThread.main()(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke()(Method.java:-2)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run()(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main()(ZygoteInit.java:858)
Run Code Online (Sandbox Code Playgroud)

Some of you might say this is a duplicate question of this, this, and this. I'm not looking for a how-to answer, rather I'm looking for an explanation why this is happening. I know this issue is because of Background Execution Limits on Android O and I already knew how to fix this by using JobIntentService. I just want to know why this crash is happening in MainActivity:onCreate().

据我了解,只有在应用程序处于后台时启动服务时才会出现此问题,例如在调用 onStop() 之后。当 MainActivity onCreate 被调用时,我的应用程序怎么可能在后台?是否有任何调用 onCreate 但应用程序仍在后台的情况?

0X0*_*gar 5

Activity仅在onResume()被调用后才被视为处于前台。这就是不允许IntentService从 from开始的原因onCreate()

引用流程和应用程序生命周期

前台进程是用户当前正在执行的操作所必需的进程。各种应用程序组件可以以不同的方式将其包含进程视为前台。如果满足以下任一条件,则进程被视为处于前台:

  • 它在用户与之交互的屏幕顶部运行一个 Activity(已调用其 onResume() 方法)。[...]

  • 更新:我发现应用程序在“onResume()”中但不在前台的场景:如果在设备屏幕锁定时启动应用程序的活动。我不知道真实用户是如何发生这种情况的,但是可以通过使用 adb 执行此操作来重现此崩溃:在应用程序停止并锁定屏幕的情况下,运行 `adb shell am start -n com.example.app/com .example.app.main.MainActivity`。我已将此信息添加到 Google 错误报告中:https://issuetracker.google.com/issues/113122354 (5认同)
  • 我的这个错误主要是由 P 设备(和一些奥利奥设备)报告的。我们的目标是 27 并从活动的 `onResume()` 中调用 `startService()`。我想知道我们是否应该将其发布到处理程序,以便在 onResume() 完成后执行?不幸的是我无法重现它。我只看到崩溃报告。 (4认同)