Android Question fused location provider (background mode) with runtime Permissions (sdk=26)

b4xscripter

Member
Licensed User
Longtime User
Hi All,

With this link I learnt to use google maps with fused location provider:

https://www.b4x.com/android/forum/threads/google-maps-with-fusedlocationprovider.83876/

Now, I would like to use it with runtimePermissions as I want to use sdk 26.

Where should I put this code for runtimePermissions? In Main?

B4X:
Sub Process_Globals

End Sub

Sub Globals
    Private gmap As GoogleMap
    Private MapFragment1 As MapFragment
    Private Panel1 As Panel
    Private rp As RuntimePermissions  ' Here I declare RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
  
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)     ' Here I ask RuntimePermissions
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
  
    Wait For MapFragment1_Ready
    gmap = MapFragment1.GetMap
    If gmap.IsInitialized Then
    Wait For LocationSource_Ready (Success As Boolean)
        If Success Then
            Log("Ready!!!")
            Dim jo As JavaObject = gmap
            jo.RunMethod("setLocationSource", Array(Starter.LocationSource))
        End If
    End If
End Sub

Sub Activity_Resume
End Sub


I get this error:

java.lang.RuntimeException: java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION


In Starter the (original) code is:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region


Sub Process_Globals
    Public flp As FusedLocationProvider
    Public LocationSource As Object
    Private LocationChangedListener As JavaObject
End Sub

Sub Service_Create
    flp.Initialize("flp")
    flp.Connect
    Wait For flp_ConnectionSuccess
    Log("FLP connected")
    Dim request As LocationRequest
    request.Initialize
    request.SetPriority(request.Priority.PRIORITY_HIGH_ACCURACY)
    request.SetInterval(2000)
    request.SetFastestInterval(1000)
    flp.RequestLocationUpdates(request)
    Dim jo As JavaObject = flp
    LocationSource = jo.CreateEventFromUI("com.google.android.gms.maps.LocationSource", "LocationSource", Null)
    CallSubDelayed2(Main, "LocationSource_Ready", True)
End Sub

Sub LocationSource_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "activate" Then
        LocationChangedListener = Args(0)
        Dim loc As Location = flp.GetLastKnownLocation
        If loc.IsInitialized Then FLP_LocationChanged(loc)
    Else if MethodName = "deactivate" Then
        LocationChangedListener = Null
    End If
    Return Null
End Sub

Sub FLP_ConnectionFailed(ConnectionResult1 As Int)
    CallSubDelayed2(Main, "LocationSource_Ready", False)
End Sub

Sub FLP_LocationChanged(Location1 As Location)
    DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
    Log(DateTime.Date(DateTime.Now))
    Log("Location Changed: " & Location1)
    If LocationChangedListener <> Null And LocationChangedListener.IsInitialized Then
        LocationChangedListener.RunMethod("onLocationChanged", Array(Location1))
    End If
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
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

In Manfest I put the permission:

B4X:
AddPermission(android.permission.ACCESS_FINE_LOCATION)

Thanks in advance!
 
Last edited:

b4xscripter

Member
Licensed User
Longtime User
Hi!

Just did it!

Main:

B4X:
#Region  Project Attributes
#ApplicationLabel: GoogleMapsLocation3
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#AdditionalJar: com.google.android.gms:play-services-maps
#AdditionalJar: com.google.android.gms:play-services-location
#End Region

#Region  Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
#BridgeLogger: true

'Activity module
Sub Process_Globals

End Sub

Sub Globals
    Private gmap As GoogleMap
    Private MapFragment1 As MapFragment
    Private Panel1 As Panel
    Private rp As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")

End Sub

Sub MapFragment1_Ready
    gmap = MapFragment1.GetMap
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
        gmap.MyLocationEnabled = Result
        CallSubDelayed(Starter, "StartFlp")
        Wait For LocationSource_Ready (Success As Boolean)           
        Log("LocationSource_Ready: " & Success)
        If Success Then
            Log("Ready!!!")
            Dim jo As JavaObject = gmap
            jo.RunMethod("setLocationSource", Array(Starter.LocationSource))
        End If
    End If

End Sub

End Sub


And Starter:


[code]

#Region  Service Attributes 
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region


Sub Process_Globals
    Public flp As FusedLocationProvider
    Public LocationSource As Object
    Private LocationChangedListener As JavaObject
End Sub

Sub Service_Create

End Sub


Sub StartFlp
    flp.Initialize("flp")
    flp.Connect
    Wait For flp_ConnectionSuccess
    Log("FLP connected")
    Dim request As LocationRequest
    request.Initialize
    request.SetPriority(request.Priority.PRIORITY_HIGH_ACCURACY)
    request.SetInterval(2000)
    request.SetFastestInterval(1000)
    flp.RequestLocationUpdates(request)
    Dim jo As JavaObject = flp
    LocationSource = jo.CreateEventFromUI("com.google.android.gms.maps.LocationSource", "LocationSource", Null)
    CallSubDelayed2(Main, "LocationSource_Ready", True)
End Sub

Sub LocationSource_Event (MethodName As String, Args() As Object) As Object
    If MethodName = "activate" Then
        LocationChangedListener = Args(0)
        Dim loc As Location = flp.GetLastKnownLocation
        If loc.IsInitialized Then FLP_LocationChanged(loc)
    Else if MethodName = "deactivate" Then
        LocationChangedListener = Null
    End If
    Return Null
End Sub

Sub FLP_ConnectionFailed(ConnectionResult1 As Int)
    CallSubDelayed2(Main, "LocationSource_Ready", False)
End Sub

Sub FLP_LocationChanged(Location1 As Location)
    DateTime.DateFormat="yyyy-MM-dd HH:mm:ss"
    Log(DateTime.Date(DateTime.Now))
    Log("Location Changed: " & Location1)
    If LocationChangedListener <> Null And LocationChangedListener.IsInitialized Then
        LocationChangedListener.RunMethod("onLocationChanged", Array(Location1))
    End If
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
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

And don't forget to change in Designer the MapFragment1's property My Location to disabled!

Thanks, Erel
 
Upvote 0
Top