Caused by android.app.ForegroundServiceStartNotAllowedException startForegroundService() not allowed due to mAllowStartForeground false

192
April 25, 2022, at 08:00 AM

I have upgrade the Android SDK project 30 to 31. Also i am using the the Android Beacon Library for scanning the the iBeacon.

Android 12 i am getting the frquently below crashes on 100% background application.

Caused by android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.beacon.android/org.altbeacon.beacon.service.BeaconService 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:6897) 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 org.altbeacon.beacon.BeaconManager.bindInternal(BeaconManager.java:438) at org.altbeacon.beacon.BeaconManager.autoBind(BeaconManager.java:1810) at org.altbeacon.beacon.BeaconManager.startMonitoring(BeaconManager.java:1136) at com.beacon.BeaconApplication.onCreate(BeaconApplication.kt:170) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7429) at android.app.ActivityThread.access$1700(ActivityThread.java:309) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2268) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8582) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133)

Currently using Altbeacon library below version

implementation 'org.altbeacon:android-beacon-library:2.19.3'

Also, below Android SDK version 30, its working absolutely fine.

Thanks in Advance !

Answer 1

This is caused by a security change to Android 12. For apps targeting SDK 31, Android 12 now blocks starting a foreground service except in very specific circumstances. Typically, your app must visible on the screen to start a foreground service.

Since this is an Android 12 security restriction, there is no way for the library to "fix" this. The solution for the developer is that you must not initiate beacon ranging or monitoring with a configured foreground service unless the app is visible in the foreground.

There is a discussion of how to make this easier on developers in an open issue with the library here

Rent Charter Buses Company
READ ALSO
Capacitor App running only for the first time

Capacitor App running only for the first time

The first time I install my capacitor app on a device the app works correctlyBut if I close my app, and open again the app don’t work anymore and get stuck on the white screen forever

94
Is there a way to know which carrier locked the android phone using ADB

Is there a way to know which carrier locked the android phone using ADB

How to get a name of a carrier that locked the android device using adb commandsI've tried with these commands so far

83
Flutter deep links not working from browser

Flutter deep links not working from browser

I made changes in Android manifes as suggested here: https://docsflutter

100