Android Question SDK31 Android12 can't executing services on background ... Error: startForegroundService() not allowed due to mAllowStartForeground false

scsjc

Well-Known Member
Licensed User
Longtime User
After installing and using sdk 31, the services that run when the application is in the background throw the following error:

Fatal Exception: java.lang.RuntimeException: Unable to start receiver com.xxxxxxxxx.starter$starter_BR: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.xxxxxxxxxx/.starter


I have read something that there is a possibility using a "work-runtime" in stackoverflow

implementation 'androidx.work:work-runtime:2.7.1'
implementation 'com.google.guava:guava:27.0.1-android'

Does anyone have a solution to be able to run in the background without error?
 

scsjc

Well-Known Member
Licensed User
Longtime User
The problem is that you mostly can no longer start services while the app is in the background. That example will actually work as it creates the foreground service while the app is in the foreground. Maybe you can do something similar.
I use a code based 100% on the example but reports error.
The example has StartServiceAt and the only solution is to disable it, right?

I thought that applying ... android:foregroundServiceType, "location" ... would not happen
B4X:
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")

how could it be improved?
 
Upvote 0

scsjc

Well-Known Member
Licensed User
Longtime User
El problema es que, en su mayoría, ya no puede iniciar los servicios mientras la aplicación está en segundo plano. Ese ejemplo realmente funcionó, ya que crea el servicio en primer plano mientras la aplicación está en primer plano. Tal vez puedas hacer algo similar.

I stopped using StartServiceAt, and it seems to less bug now.
Do you think there could be solutions so that the process stays in the background...

-> StartServiceOnExact (<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />)
-> Disable battery optimizations for the app (ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top