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 但应用程序仍在后台的情况?
您Activity仅在onResume()被调用后才被视为处于前台。这就是不允许IntentService从 from开始的原因onCreate()
前台进程是用户当前正在执行的操作所必需的进程。各种应用程序组件可以以不同的方式将其包含进程视为前台。如果满足以下任一条件,则进程被视为处于前台:
- 它在用户与之交互的屏幕顶部运行一个 Activity(已调用其 onResume() 方法)。[...]
| 归档时间: |
|
| 查看次数: |
3059 次 |
| 最近记录: |