Android Question startForegroundService not allowed due to mAllowStartForeground

samannnn

Member
i use a service to get some data from my online database when the app is starts and start admob . i have an error in androis 13 and above:

B4X:
Fatal Exception: java.lang.RuntimeException
android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service ir.samware.ludo/.http
anywheresoftware.b4a.keywords.Common$12.run
Caused by android.app.ForegroundServiceStartNotAllowedException
startForegroundService() not allowed due to mAllowStartForeground false: service test.test.test/.http
android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)

i call my service in the Main Activity
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime=True Then
        StartService(http)
    End If
End Sub

B4X:
ub Service_Create
'    Dim MobileAds As MobileAds
'    MobileAds.Initialize
 
    CheckForGooglePlayServices
    DeviceInfo
End Sub

Sub Service_Start (StartingIntent As Intent)
        If analytics.IsGooglePlayServicesAvailable=True And SDKver>=14 Then
            Log("GooglePlayServicesAvailable  ="&analytics.IsGooglePlayServicesAvailable)
            analytics.Initialize
            analytics.SendEvent("login", CreateMap ("additional parameter": 100))
        End If
 
    Service.StopAutomaticForeground
End Sub

Sub Service_Destroy

End Sub

i search and found that i should add
B4X:
SetServiceAttribute(MyService, "android:foregroundServiceType", "dataSync")
could you help me by putting this line to manifest the problem is solved?
 

samannnn

Member
Do you actually need the service to be a foreground service? Does it need to run while the app is in the background?

If this is a new app then first thing to do is to switch to B4XPages.
this is not new app. i dont need forground service. how should i do that
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Make sure not to call Service.StartForeground.

Add this to the manifest editor:
B4X:
SetServiceAttribute(YourService, android:foregroundServiceType, shortService)

And to the service:
B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(51042) 'this is the id of the automatic foreground notification
End Sub
 
Upvote 0

samannnn

Member
Make sure not to call Service.StartForeground.

Add this to the manifest editor:
B4X:
SetServiceAttribute(YourService, android:foregroundServiceType, shortService)

And to the service:
B4X:
Private Sub Service_Timeout(Params As Map)
    Service.StopForeground(51042) 'this is the id of the automatic foreground notification
End Sub
i have this errorr on starter service should i do that?

B4X:
Fatal Exception: java.lang.RuntimeException
Unable to start service test.test.test.starter@9eb8925 with Intent { cmp=test.test.test/.starter (has extras) }: java.lang.RuntimeException: android.app.MissingForegroundServiceTypeException: Starting FGS without a type callerApp=ProcessRecord{99b6a0 26192:test.test.test/u0a312} targetSDK=35
 
Upvote 0
Top