android.app.ForegroundServiceStartNotAllowedException:
Bug Summary
We found a lot of android.app.ForegroundServiceStartNotAllowedException in the google play console after upgrade target 31. Most issue traces are in below two places: com.smartdevicelink.transport.SdlBroadcastReceiver.pingRouterService (SdlBroadcastReceiver.java:477) and at com.smartdevicelink.transport.SdlBroadcastReceiver.startRouterService (SdlBroadcastReceiver.java:275)
I see from the code that it just catch the SecurityException only. it should also catch ForegroundServiceStartNotAllowedException for android 12 or above.
Reproduction Steps
- Integartion with SDL in the app with latest version(5.4.0) to support the target 31
Expected Behavior: Should work as normal Observed Behavior: A lot of crash were found in google play consle after app upgrade to support target 31
Which projects have you seen this bug on?
Android
Android Version(s)
Android 12 and above
Android Device(s)
No response
sdl_java_suite Version
5.4.0
Testing Environment(s)
It seems happend just at the starting service of SDL when BT connect. Not real connect the Head Unit.
Relevant log output
android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable (Parcel.java:3399)
at android.os.Parcel.createExceptionOrNull (Parcel.java:2461)
at android.os.Parcel.createException (Parcel.java:2448)
at android.os.Parcel.readException (Parcel.java:2431)
at android.os.Parcel.readException (Parcel.java:2373)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:5971)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1847)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1823)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:779)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:779)
at com.smartdevicelink.transport.SdlBroadcastReceiver.startRouterService (SdlBroadcastReceiver.java:275)
at com.smartdevicelink.transport.SdlBroadcastReceiver.access$400 (SdlBroadcastReceiver.java:72)
at com.smartdevicelink.transport.SdlBroadcastReceiver$2.onComplete (SdlBroadcastReceiver.java:342)
at com.smartdevicelink.util.ServiceFinder.onFinished (ServiceFinder.java:122)
at com.smartdevicelink.util.ServiceFinder.access$000 (ServiceFinder.java:56)
at com.smartdevicelink.util.ServiceFinder$1.run (ServiceFinder.java:82)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:346)
at android.os.Looper.loop (Looper.java:475)
at android.app.ActivityThread.main (ActivityThread.java:7889)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1009)
Caused by: android.os.RemoteException:
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:734)
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:653)
at com.android.server.am.ActivityManagerService.startService (ActivityManagerService.java:12328)
at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:2519)
at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:2560)
And
java.lang.RuntimeException:
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args (LoadedApk.java:1815)
at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run (Unknown Source:2)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8663)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by: android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable (Parcel.java:3345)
at android.os.Parcel.createExceptionOrNull (Parcel.java:2432)
at android.os.Parcel.createException (Parcel.java:2421)
at android.os.Parcel.readException (Parcel.java:2404)
at android.os.Parcel.readException (Parcel.java:2346)
at android.app.IActivityManager$Stub$Proxy.startService (IActivityManager.java:6914)
at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1926)
at android.app.ContextImpl.startForegroundService (ContextImpl.java:1892)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:796)
at android.content.ContextWrapper.startForegroundService (ContextWrapper.java:796)
at com.smartdevicelink.transport.SdlBroadcastReceiver.pingRouterService (SdlBroadcastReceiver.java:477)
at com.smartdevicelink.transport.SdlBroadcastReceiver$2.onComplete (SdlBroadcastReceiver.java:360)
at com.smartdevicelink.util.ServiceFinder.onFinished (ServiceFinder.java:122)
at com.smartdevicelink.util.ServiceFinder.access$000 (ServiceFinder.java:56)
at com.smartdevicelink.util.ServiceFinder$2.onReceive (ServiceFinder.java:113)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args (LoadedApk.java:1805)
at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run (Unknown Source:2)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:226)
at android.os.Looper.loop (Looper.java:313)
at android.app.ActivityThread.main (ActivityThread.java:8663)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by: android.os.RemoteException:
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:771)
at com.android.server.am.ActiveServices.startServiceLocked (ActiveServices.java:679)
at com.android.server.am.ActivityManagerService.startService (ActivityManagerService.java:14071)
at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:2943)
at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:3034)
Hi, with Android 12 we did change how SdlService needs to get started. Can you verify that you are starting your SdlService like we have in our migration guide here: https://smartdevicelink.com/en/guides/android/migrating-to-newer-sdl-java-suite-versions/updating-to-v54/#starting-services-from-the-foreground
Yes, we did this implementation as doc says.
I've been seeing this happen on Android 12 devices for months now (albeit not running SDL stuff), and particularly on Samsung: https://stackoverflow.com/questions/70711950/android-12-foregroundservicestartnotallowedexception-while-in-foreground
I believe this to be an Android bug, but if anyone figures it out pls let me know
Closing with #1823.
While the fix does not prevent the exception from happening, it will prevent the app from crashing. Unfortunately the bug itself does seem out of our control. I believe on some devices the allocation of time to have permission to start a foreground service might be altered to be lower, or it simply takes too long to start said service. There is no way to guarantee that we perform the start prior to the threshold expiring and therefore the best we can do currently is to catch the exception.