android - java.lang.IllegalStateException : Not allowed to start service Intent (from Activity onCreate)

标签 android illegalstateexception android-8.0-oreo android-intentservice

我从 MainActivity:onCreate 启动 IntentService,并且我从上面仅在 Android Oreo 上的崩溃报告中注意到此崩溃:

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)

你们中的一些人可能会说这是 this 的重复问题。 , this ,和 this 。我不是在寻找如何回答,而是在寻找为什么会发生这种情况的解释。我知道这个问题是因为Background Execution Limits在 Android O 上,我已经知道如何使用 JobIntentService 来解决这个问题。我只是想知道为什么会在 MainActivity:onCreate() 中发生崩溃。

据我了解,此问题应该仅在应用程序处于后台时启动服务时发生,例如在调用 onStop() 后。当调用 MainActivity onCreate 时,我的应用程序怎么可能处于后台?是否有调用onCreate但app仍在后台的场景?

最佳答案

只有在调用 onResume() 后,您的 Activity 才会被视为位于前台。这就是为什么不允许从 onCreate() 启动 IntentService

引用自Processes and Application Lifecycle

A foreground process is one that is required for what the user is currently doing. Various application components can cause its containing process to be considered foreground in different ways. A process is considered to be in the foreground if any of the following conditions hold:

  • It is running an Activity at the top of the screen that the user is interacting with (its onResume() method has been called).[...]

关于android - java.lang.IllegalStateException : Not allowed to start service Intent (from Activity onCreate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51863600/

相关文章:

android - 试图重新打开一个已经关闭的对象 : java. lang.IllegalStateException :?

java - 我可以使用运行时参数来修复 Java 中的错误 API 调用吗?

android-studio-3.0 - Android 键盘 (AOSP) 一直停止使用 Android O 模拟器

android - NotificationCompat v7 和 Android O

android - 为什么 Android O 失败并显示 "does not belong to this FragmentManager!"

android - 检查 Android 较低 api 兼容性的代码

android - 无法写入 Android 中的 SQLite 数据库

android - 半透明状态栏使布局越界(显示后面的 View )

android - 获取异常 "IllegalStateException: Can not perform this action after onSaveInstanceState"

android - 纵向模式下的 MediaRecorder 视频捕获