Service.startForeground() not allowed due to mAllowStartForeground false, Android 14
Asked Answered
D

1

6

I have an app that uses a sticky foreground service to do some Bluetooth operations, it was working well untill android 14 release.
I got this trace from Firebase although I am not able to reproduce.

Caused by android.app.ForegroundServiceStartNotAllowedException

Service.startForeground() not allowed due to mAllowStartForeground false: service xx.xxx.ForegroundService

android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)

android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)

android.os.Parcel.readParcelableInternal (Parcel.java:4882)

android.os.Parcel.readParcelable (Parcel.java:4864)

android.os.Parcel.createExceptionOrNull (Parcel.java:3064)

android.os.Parcel.createException (Parcel.java:3053)

android.os.Parcel.readException (Parcel.java:3036)

android.os.Parcel.readException (Parcel.java:2978)

android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7214)

android.app.Service.startForeground (Service.java:775)

xx.xxx.ForegroundService.onStartCommand (ForegroundService.kt:57)

android.app.ActivityThread.handleServiceArgs (ActivityThread.java:5268)

android.app.ActivityThread.-$$Nest$mhandleServiceArgs

android.app.ActivityThread$H.handleMessage (ActivityThread.java:2531)

android.os.Handler.dispatchMessage (Handler.java:106)

android.os.Looper.loopOnce (Looper.java:230)

android.os.Looper.loop (Looper.java:319)

android.app.ActivityThread.main (ActivityThread.java:8893)

java.lang.reflect.Method.invoke (Method.java)

com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:608)

com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1103)

I have updated my app to meet android 14 service requirements, but the crash still persist.
Also as I am aware, this is not the error mentioned in the documentation when not meeting android 14 foreground service requirements.

I have found this issue: https://issuetracker.google.com/issues/307329994

Any help would be appreciated!

Dilative answered 18/12, 2023 at 15:57 Comment(7)
Does this answer your question? Fatal Android 12: Exception: startForegroundService() not allowed due to mAllowStartForeground falseLashondalashonde
Please provide enough code so others can better understand or reproduce the problem.Disembark
@emandt, I don't think this is my case. As this crash is exclusive for Android 14, and never happened on Android 12 nor 13Dilative
@AmeerElbaz plz read the ACCEPTED answer at that link and not only the others comments: "Apps that target Android 12 (API level 31) or higher can't start foreground services while running in the background........"Lashondalashonde
@Lashondalashonde I have exactly the same issue as OP and agree with his comments. The linked question is related to an issue for apps targeting Android 12 and higher. This issue is related to apps running on Android 14 (regardless of target version). So they are two separate things. My app targets Android 13, but I still see this issue only occurring for users who are on Android 14.Claar
Exactly as @AdamBurley explained. Also even calling the service from MainActivity causes the same crash in crashlytics. The interesting point is that the crash starts to appear after 1-2 days of the app being live. May be I should try returning some thing else rather than START_STICKY? Actually the testing takes time, as I can not reproduce myself.Dilative
According to the issue report linked from your question, returning START_NOT_STICKY will resolve the problem. However it's not a good option for us, because we need the service to be sticky.Claar
L
1

The issue descends from "on newer Android a background service cannot start a service".

When service's process is killed by system due to low memory or for any other reason, it'll try to recreate the service by passing NULL intent. Previously we could call startForeground() from background in START_STICKY restart scenario, but looks like it's removed in AND 14.

Since startForeground() is required only after calling startForegroundService, we don't need to call in this particular case.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null) {
        startForeground();    //or wherever you call it: check the Intent before!
    }
}
Lashondalashonde answered 8/2 at 22:29 Comment(1)
You are right, the problem is with START_STICKY. But this behavior is required by many apps. Furthermore it is not mentioned anywhere that it is removed in AND 14. I hope it is not. I wonder why we can't reproduce locally (All reports from crashlytics), do you have any idea?Dilative

© 2022 - 2024 — McMap. All rights reserved.