Android Question java.lang.SecurityException: Starting FGS with type location callerApp=......

Almora

Well-Known Member
Licensed User
Longtime User
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    Public flp As FusedLocationProvider
    Private nid As Int = 1
    Private lock As PhoneWakeState
End Sub

Sub Service_Create
    flp.Initialize("flp")
    flp.Connect
    lock.PartialLock
End Sub

Sub flp_ConnectionSuccess
    Log("Connected to location provider")
End Sub

Sub flp_ConnectionFailed(ConnectionResult1 As Int)
    Log("Failed to connect to location provider")
End Sub

Sub Service_Start (StartingIntent As Intent)
'    Service.StopAutomaticForeground 'Starter service can start in the foreground state in some edge cases.
    Service.StartForeground(nid, CreateNotification("waiting.."))
End Sub

Sub CheckLocationSettingStatus As ResumableSub
    Dim f As LocationSettingsRequestBuilder
    f.Initialize
    f.AddLocationRequest(CreateLocationRequest)
    flp.CheckLocationSettings(f.Build)
    Wait For flp_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
    Return LocationSettingsResult1
End Sub

Public Sub StartLocationUpdates
    flp.RequestLocationUpdates(CreateLocationRequest)
   
End Sub

Private Sub flp_LocationChanged (Location1 As Location)
    CallSub2(Main, "Location_Changed", Location1)
   
    Dim body As String = $"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$
    Dim n As Notification = CreateNotification(body)
    n.Notify(nid)
    Log(body)
   
    Log(Location1.Latitude)
End Sub

Private Sub CreateLocationRequest As LocationRequest
    Dim lr As LocationRequest
    lr.Initialize
    lr.SetInterval(0)
    Return lr
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub


Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Location", Body, Main)
    Return notification
End Sub

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="34"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)
'End of default text.
'AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)
    
     AddPermission(android.permission.FOREGROUND_SERVICE_LOCATION)
'     SetServiceAttribute(Starter, android:foregroundServiceType, "location")





hello
I added a notification to the application. I get this error. with sdk 34..
I couldn't solve this problem. I want to use FLP.

AddPermission(android.permission.FOREGROUND_SERVICE_LOCATION)
added.



error
starter_service_start (java line: 297)
java.lang.SecurityException: Starting FGS with type location callerApp=ProcessRecord{81724a5 11342:b4a.example/u0a224} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_LOCATION] any of the permissions allOf=false [android.permission.ACCESS_COARSE_LOCATION, android.permission.ACCESS_FINE_LOCATION] and the app must be in the eligible state/exemptions to access the foreground only permission
at android.os.Parcel.createExceptionOrNull(Parcel.java:3242)
at android.os.Parcel.createException(Parcel.java:3226)
at android.os.Parcel.readException(Parcel.java:3209)
at android.os.Parcel.readException(Parcel.java:3151)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7193)
at android.app.Service.startForeground(Service.java:776)
at anywheresoftware.b4a.objects.ServiceHelper.StartForeground(ServiceHelper.java:85)
at b4a.example.starter._service_start(starter.java:297)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:201)
at b4a.example.starter.handleStart(starter.java:103)
at b4a.example.starter.access$000(starter.java:8)
at b4a.example.starter$1.run(starter.java:74)
at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:240)
at b4a.example.starter.onStartCommand(starter.java:72)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5147)
at android.app.ActivityThread.-$$Nest$mhandleServiceArgs(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2469)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8705)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActiveServices.validateForegroundServiceType(ActiveServices.java:2905)
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2589)
at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1843)
at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:14092)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3594)
 
Top