Android Question FusedLocationProvider, how to re show Resolution Dialog?

incendio

Well-Known Member
Licensed User
Hi guys,

I wrote background application tracking via service. Here are my codes in service
B4X:
#StartAtBoot: True
Sub Process_Globals
    Public FLP As FusedLocationProvider
    Private nid As Int = 1
    Private b As Beeper
    Private LastLoc As Location
    Private Distance As Float
End Sub

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

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER

    b.Initialize(300, 1000)
    LastLoc.Initialize
    LastLoc.Latitude = 0
    FLP.Initialize("flp")
    FLP.Connect
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("Active"))

    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)

    If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
        ToastMessageShow("No GPS!", True)
    End If
End Sub

Sub Service_Destroy
End Sub

Sub flp_ConnectionSuccess
    Private LocationRequest=CreateLocationRequest As LocationRequest
    
    Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
    LocationSettingsRequestBuilder1.Initialize
    LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest)
    FLP.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)
    FLP.RequestLocationUpdates(LocationRequest)

    Log("Connected to location provider")
End Sub

Private Sub CreateLocationRequest As LocationRequest
    Dim lr As LocationRequest
    lr.Initialize
    lr.SetInterval(5000)
    lr.SetSmallestDisplacement(1)
    lr.SetFastestInterval(lr.GetInterval / 2)
    lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
    
    Return lr
End Sub

Sub FLP_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
    Log("FusedLocationProvider1_LocationSettingsChecked")
    Dim LocationSettingsStatus1 As LocationSettingsStatus=LocationSettingsResult1.GetLocationSettingsStatus
    Select LocationSettingsStatus1.GetStatusCode
        Case LocationSettingsStatus1.StatusCodes.RESOLUTION_REQUIRED
            Log("RESOLUTION_REQUIRED")
            CallSubDelayed2(modMain, "HandleLocationSettingsStatus", LocationSettingsStatus1)
        Case LocationSettingsStatus1.StatusCodes.SETTINGS_CHANGE_UNAVAILABLE
            Log("SETTINGS_CHANGE_UNAVAILABLE")
            CallSubDelayed2(modMain, "HandleLocationSettingsStatus", LocationSettingsStatus1)
        Case LocationSettingsStatus1.StatusCodes.SUCCESS
            Log("SUCCESS")
    End Select
End Sub

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

Private Sub flp_LocationChanged (Loc As Location)
    b.beep
    LastLoc = Loc
End Sub

Sub FLP_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
    
    Select SuspendedCause1
        Case FLP.SuspendedCause.CAUSE_NETWORK_LOST
            ToastMessageShow("Network lost",True)
        Case FLP.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            ToastMessageShow("Service Disconected",True)
    End Select
End Sub

And here are the codes to display Resolution Dialog in modMain
B4X:
Sub HandleLocationSettingsStatus(LocationSettingsStatus1 As LocationSettingsStatus)
    Select LocationSettingsStatus1.GetStatusCode
        Case LocationSettingsStatus1.StatusCodes.RESOLUTION_REQUIRED
            Log("RESOLUTION_REQUIRED")
            LocationSettingsStatus1.StartResolutionDialog("LocationSettingsResult1")
        Case LocationSettingsStatus1.StatusCodes.SETTINGS_CHANGE_UNAVAILABLE
            Log("SETTINGS_CHANGE_UNAVAILABLE")
            Msgbox("No Internet access!.", "Error")
            Activity.Finish
    End Select
End Sub

Sub LocationSettingsResult1_ResolutionDialogDismissed(LocationSettingsUpdated As Boolean)
    Log("LocationSettingsResult1_ResolutionDialogDismissed")
    If Not(LocationSettingsUpdated) Then
        '    the user failed to update the device settings to meet the location request requirements
        Msgbox("No GPS access", "Error")
        Activity.Finish
    End If
End Sub

With those codes, app runs OK, but there is a problem if sometime later, users, not intentionally, turn off GPS.
The app can't track users location.

How to detect if GPS turn off and re show Resolution Dialog again in this case?
 

Xicu

Active Member
Licensed User
To verify that the user has the location active on the device, simply add the Phone library, and verify that the GetSettings property returns the "GPS" or "network" or "network, GPS" modes that correspond respectively to the mode types location "Device Only", "Energy saving", or "High precision". (Selectable in Configuration --- Security and location ---- Location ---- Mode)

If it returns an empty string then the user does not have the active location.
B4X:
Dim p As Phone
If p.GetSettings("location_providers_allowed")="" Then
    ToastMessageShow("Ubicacion no activada",True)
else
    ToastMessageShow("Ubicacion activada,True)
End If


other possibility is using Gps library
B4X:
If GPS1.GPSEnabled = False Then
    ToastMessageShow("Gps not active",True)
else
    ToastMessageShow("Gps active",True)
End If

I prefer de first
 
Last edited:
Upvote 0

Xicu

Active Member
Licensed User
How to detect if GPS turn off and re show Resolution Dialog again in this case

I don't know if this answers your question

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER

    b.Initialize(300, 1000)
    LastLoc.Initialize
    LastLoc.Latitude = 0
    FLP.Initialize("flp")

   '1- check that we have location permissions activated
    If Starter.rp.Check(Starter.RP.PERMISSION_ACCESS_FINE_LOCATION) = False Then Return
   
    '2- we check that we have the location activated
    Dim p As Phone
    If p.GetSettings ("location_providers_allowed")= "" Then
        're show Resolution Dialog here
        Log("Location off")
        Return
    Else
        FLP.Connect
    End if
End Sub
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
To verify that the user has the location active on the device, simply add the Phone library, and verify that the GetSettings property returns the "GPS" or "network" or "network, GPS" modes that correspond respectively to the mode types location "Device Only", "Energy saving", or "High precision". (Selectable in Configuration --- Security and location ---- Location ---- Mode)

If it returns an empty string then the user does not have the active location.
B4X:
Dim p As Phone
If p.GetSettings("location_providers_allowed")="" Then
    ToastMessageShow("Ubicacion no activada",True)
else
    ToastMessageShow("Ubicacion activada,True)
End If


other possibility is using Gps library
B4X:
If GPS1.GPSEnabled = False Then
    ToastMessageShow("Gps not active",True)
else
    ToastMessageShow("Gps active",True)
End If

I prefer de first

Thanks for your answer, I will test it soon.
 
Upvote 0

incendio

Well-Known Member
Licensed User
I don't know if this answers your question

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER

    b.Initialize(300, 1000)
    LastLoc.Initialize
    LastLoc.Latitude = 0
    FLP.Initialize("flp")

   '1- check that we have location permissions activated
    If Starter.rp.Check(Starter.RP.PERMISSION_ACCESS_FINE_LOCATION) = False Then Return
  
    '2- we check that we have the location activated
    Dim p As Phone
    If p.GetSettings ("location_providers_allowed")= "" Then
        're show Resolution Dialog here
        Log("Location off")
        Return
    Else
        FLP.Connect
    End if
End Sub
Correct me if I wrong, isn't codes in service create only executed once?

So if in the first time ap runs, users already turns on GPS and then later, after the app runs, user turn off GPS, the codes in service create won't execute again, so Resolution dialog won't shows to user.
 
Upvote 0

incendio

Well-Known Member
Licensed User
I add the Timer to check for GPS status
B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER

    b.Initialize(300, 1000)
    LastLoc.Initialize
    LastLoc.Latitude = 0
    FLP.Initialize("flp")
    FLP.Connect
    Tmr.Initialize("Tmr",60000)
End Sub

Sub Tmr_Tick
    Tmr.Enabled = False
    Dim p As Phone
    If p.GetSettings("location_providers_allowed")="" Then
        b.Beep
        ToastMessageShow("no GPS", True)
        StartActivity(modMain)
        flp_ConnectionSuccess
    End If
End Sub

Sub flp_ConnectionSuccess
    Private LocationRequest=CreateLocationRequest As LocationRequest
    
    Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
    LocationSettingsRequestBuilder1.Initialize
    LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest)
    FLP.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)
    FLP.RequestLocationUpdates(LocationRequest)

    Log("Connected to location provider")
    Tmr.Enabled = True
End Sub

It worked, but I don't know if that was the best solution.

Can anyone confirm that that codes is the solution?
 
Upvote 0
Top