Fixed
Status Update
Comments
ma...@lifx.co <ma...@lifx.co> #2
I should have changed the title of this bug report.
MainActivity.onResume() calling startService caused crash, app still thinks it is in background
MainActivity.onResume() calling startService caused crash, app still thinks it is in background
dn...@google.com <dn...@google.com>
dn...@google.com <dn...@google.com> #3
Thank you for reporting this issue. For us to further investigate this issue, please provide the following additional information:
Steps to reproduce
What steps are needed to reproduce this issue?
Please explain the reproduction steps in detail.
Android bug report capturing
After reproducing the issue, press the volume up, volume down, and power button simultaneously. This will capture a bug report on your device in the “bug reports” directory. Attach the bug report file to this issue.
Alternate method
After reproducing the issue, navigate to “developer settings”, ensure “USB debugging” is enabled, then enable “Bug report shortcut”. Capture bug report by holding the power button and selecting the “Take bug report” option.
Note: Please upload the files to google drive and share the folder to android-bugreport@google.com, then share the link here.
Steps to reproduce
What steps are needed to reproduce this issue?
Please explain the reproduction steps in detail.
Android bug report capturing
After reproducing the issue, press the volume up, volume down, and power button simultaneously. This will capture a bug report on your device in the “bug reports” directory. Attach the bug report file to this issue.
Alternate method
After reproducing the issue, navigate to “developer settings”, ensure “USB debugging” is enabled, then enable “Bug report shortcut”. Capture bug report by holding the power button and selecting the “Take bug report” option.
Note: Please upload the files to google drive and share the folder to android-bugreport@google.com, then share the link here.
ma...@lifx.co <ma...@lifx.co> #4
We've tried extensively to reproduce this crash but have been unable to do so.
Can you provide any context for how the "app is in background" condition would occur inside MainActivity.onResume?
Can you provide any context for how the "app is in background" condition would occur inside MainActivity.onResume?
dn...@google.com <dn...@google.com> #5
We have passed this to the development team and will update this issue with more information as it becomes available.
[Deleted User] <[Deleted User]> #6
We've also seen this in crash logs from the wild, only on P, no repro case yet, but for us it does seem limited to activities that are shown as a result of a SEND intent. For example a user selects our app when sharing from another, our activity launches, kicks off a short service to process the share and closes the activity fairly quickly (sometimes before Activity.onCreate completes). Given that an Activity was created here, we would expect we would have a few minutes of being considered a foreground app and be able to launch a short lived service in this case.
dn...@google.com <dn...@google.com> #7
We've deferred this issue for consideration in a future release. Thank you for your time to make Android better.
dn...@google.com <dn...@google.com> #8
In case you want to provide more information with respect to this bug, please file a bug in AOSP via "https://goo.gl/TbMiIO ".
is...@google.com <is...@google.com>
ad...@google.com <ad...@google.com> #9
The issue has been addressed in future Android release.
There is a workaround to avoid application crash. Applications can get the process state in Activity.onResume() by calling ActivityManager.getRunningAppProcesses() and avoid starting Service if the importance level is lower than ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
There is a workaround to avoid application crash. Applications can get the process state in Activity.onResume() by calling ActivityManager.getRunningAppProcesses() and avoid starting Service if the importance level is lower than ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
[Deleted User] <[Deleted User]> #10
> If the device hasn’t fully awake, activities would be paused immediately and eventually be resumed again after its fully awake.
Is it guaranteed that the onResume will fire again after the device is fully awake? Also, in case of using the ActivityLifecycleCallbacks - is it also guaranteed that onActivityResumed will be called again after the device is fully awake?
Is it guaranteed that the onResume will fire again after the device is fully awake? Also, in case of using the ActivityLifecycleCallbacks - is it also guaranteed that onActivityResumed will be called again after the device is fully awake?
mi...@gmail.com <mi...@gmail.com> #11
I'm afraid the workaround described in #9 doesn't work. I have the following code in my Activity.onResume() method and I still see crash reports:
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningAppProcessInfo = activityManager.runningAppProcesses?.first { it.pid == Process.myPid() }
if (runningAppProcessInfo != null && runningAppProcessInfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
startMyForegroundService() // App still crashes here: IllegalStateException: Not allowed to start service Intent { ... }: app is in background
}
val activityManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
val runningAppProcessInfo = activityManager.runningAppProcesses?.first { it.pid == Process.myPid() }
if (runningAppProcessInfo != null && runningAppProcessInfo.importance >= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
startMyForegroundService() // App still crashes here: IllegalStateException: Not allowed to start service Intent { ... }: app is in background
}
an...@gmail.com <an...@gmail.com> #12
As I see, we need to write importance comparison this way:
runningAppProcessInfo.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
(less or equal) - the lower importance int value = the higher importance
runningAppProcessInfo.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
(less or equal) - the lower importance int value = the higher importance
[Deleted User] <[Deleted User]> #13
When I try using the workaround my App fails to start a service after a device restart. Before doing 'ContextCompat.startForegroundService' I check if the importance level is correct with 'runningAppProcessInfo.importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND'. This has removed the error reports of "Not allowed to start service Intent". However, I've added the check to every launch of the service, and when I try to launch the service when receiving a 'QUICKBOOT_POWERON'-Broadcast (aka when the device was rebooted) I get an importance of 300 and thus the service doesn't launch because of this workaround. When I temporarily disabled the check I was able to do 'ContextCompat.startForegroundService' and it launched the Service without any problem.
Is there something I'm missing here? I thought a foreground Service could only be launched when the importance level is '<= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND', or at least that's what this workaround suggests.
Is there something I'm missing here? I thought a foreground Service could only be launched when the importance level is '<= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND', or at least that's what this workaround suggests.
th...@gmail.com <th...@gmail.com> #14
Is this been fixed yet? If so how do I get access to it. It is also in Android 10.
fm...@gmail.com <fm...@gmail.com> #15
What's the resolution for this? Google literally has a bound service not working properly...
[Deleted User] <[Deleted User]> #16
Comment has been deleted.
or...@gmail.com <or...@gmail.com> #17
This issue is reproduced on Google Pixel 3 XL running Android 11.
ef...@gmail.com <ef...@gmail.com> #18
Got a report for this issue for a device "Android 11 - POCO X4 Pro 5G".
The only reliable way how I can replicate the issue (on Android 12 as well):
- lock screen on device
- in android studio press "Run"
AppLifecycleObserver reports that app is in foreground state, but onResume gets called (while screen is still locked) and the service launch crashes.
This workaround worked for me:https://stackoverflow.com/a/61323318/2348614
The only reliable way how I can replicate the issue (on Android 12 as well):
- lock screen on device
- in android studio press "Run"
AppLifecycleObserver reports that app is in foreground state, but onResume gets called (while screen is still locked) and the service launch crashes.
This workaround worked for me:
Description
Getting crash reports from HockeyApp, getting OS Version reported as "P" and "9". Haven't been able to reproduce this issue on test devices yet, but this seems to only occur in some circumstances. Have received 122 crashes from 93 users over the last ~2 weeks.
There seem to be a number of similar reports for this issue, all of which seem to be using startForegroundService(), where as we are using startService() and it was working successfully on all versions including Android 8.x.
The stack trace that I've attached below seems to indicate this is happening on the MainActivity.onResume() being called and the app still considering itself to be in the background and then crashing because it can't start the service while it is in the background.
* Is this a regression from O to P?
Seems to be a regression or a policy change as this crash isn't occurring at all for Android 8.x
* What device are you using? (for example, Pixel XL)
Pixel XL
* What are the steps to reproduce the problem? (Please provide the minimal reproducible test case.)
Haven't found any reproduction steps as yet.
* Issue Category e.g. Framework (platform), NDK (platform), Hardware (CPU, GPU, Sensor, Camera), ART (platform), Runtime Permissions etc
Framework
* What was the expected result?
No crash, service starts like on Android O
* Can you provide the API document where this expected behavior is explained?
* What was the actual result?
java.lang.RuntimeException: Unable to resume activity {com.lifx.lifx/com.lifx.app.MainActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.lifx.lifx/.service.LifxService (has extras) }: app is in background uid UidRecord{a3ea2ac u0a163 TPSL bg:+8m32s394ms idle change:cached procs:1 seq(0,0,0)}
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3796)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3828)
at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:160)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:72)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1800)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.lifx.lifx/.service.LifxService (has extras) }: app is in background uid UidRecord{a3ea2ac u0a163 TPSL bg:+8m32s394ms idle change:cached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1561)
at android.app.ContextImpl.startService(ContextImpl.java:1516)
at android.content.ContextWrapper.startService(ContextWrapper.java:657)
at com.lifx.app.MainActivity.onResume(MainActivity.java:500)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1403)
at android.app.Activity.performResume(Activity.java:7286)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3788)
... 11 more