#Region Service Attributes
' I don't see a reason for starting this at boot.
#StartAtBoot: False
#End Region
Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Public rp As RuntimePermissions
Public FLP As FusedLocationProvider
Private flpStarted As Boolean
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
FLP.Initialize("flp")
'FLP.Connect ' Don't do this here. If this, for some reason, takes a long time, you may get an ANR
lock.PartialLock
End Sub
Sub flp_ConnectionSuccess
Log("Connected to location provider")
StartFLP ' Might as well use the event to call StartFLP properly
End Sub
Sub flp_ConnectionFailed(ConnectionResult1 As Int)
Log($"Failed to connect to location provider. ConnectionResult = ${ConnectionResult1}"$)
StopService(Me) ' Kill myself for cleanup
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
'We are only going to call StartFLP directly if we're already connected
If FLP.IsConnecting Then
Log("Already trying to connect")
Else if FLP.IsConnected Then
StartFLP
Else
FLP.Connect
End If
'StartFLP
End Sub
'Taken straight from the example provided by @Erel
'https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
Private 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
'Modified to include extra code from
''https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
Public Sub StartFLP
If flpStarted Then Return
If rp.Check(rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then ' Use locally declared rp
Log("No permission")
Return
End If
'We only call StartFLP when FLP.isConnected = True. See Service_Start
' Do While FLP.IsConnected = False
' Sleep(1000)
' Loop
Log(flpStarted)
If flpStarted = False Then
'See
Wait For (CheckLocationSettingStatus) Complete (SettingsResult As LocationSettingsResult)
Dim sc As StatusCodes
Select SettingsResult.GetLocationSettingsStatus.GetStatusCode
Case sc.SUCCESS
RequestLocationUpdates
Case sc.RESOLUTION_REQUIRED
'see https://www.b4x.com/android/forum/threads/fusedlocationprovider-resolution-dialog.111652/#content
Log("Location services require resolution")
'Adjust implementation as needed
' Dim rs As ResumableSub = CallSub2(Main, "ShowResolutionDialog", SettingsResult.GetLocationSettingsStatus)
' Dim Result As Boolean
' If rs.IsInitialized Then
' Wait For (rs) Complete (LocationSettingsUpdated As Boolean)
' Result = LocationSettingsUpdated
' End If
' If Result Then
' SettingsAreGood
' Else
' SetState("Not enabled")
' End If
Case Else
Log("Location services not available/enabled")
End Select
End If
flpStarted = True
End Sub
Private Sub RequestLocationUpdates
FLP.RequestLocationUpdates(CreateLocationRequest)
flpStarted = True
End Sub
Private Sub flp_LocationChanged (Location1 As Location)
If Not(IsPaused(Main)) Then ' Let's make sure Main is up and running
CallSub2(Main, "Location_Changed", Location1)
End If
End Sub
Private Sub CreateLocationRequest As LocationRequest
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(10000)
lr.SetFastestInterval(lr.GetInterval / 2)
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
Return lr
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tracking location", Body, Main)
Return notification
End Sub
Sub Service_Destroy
If flpStarted Then
FLP.RemoveLocationUpdates
FLP.Disconnect ' A little bit more of a cleanup
flpStarted = False
End If
lock.ReleasePartialLock
Service.StopForeground(nid) ' Stopping what we started
End Sub