#Region Service Attributes
#StartAtBoot: True
#End Region
#StartCommandReturnValue: android.app.Service.START_STICKY
Sub Process_Globals
Private nid As Int = 1
Private GPS As GPS
Private Tracking As Boolean
Private LastUpdateTime As Long
Private lock As PhoneWakeState
Dim gpslong, gpslat As Double
Dim tmrTracker As Timer
Dim counter As Int
Dim rp As RuntimePermissions
Dim LastLocation As Location
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
GPS.Initialize("gps")
lock.PartialLock
Try
tmrTracker.Initialize("Timer1",1000)
Catch
Log(LastException)
End Try
tmrTracker.Enabled=True
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
Log("Tracker Service start")
Track
End Sub
Public Sub Track
If Tracking Then Return
If rp.Check(rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If
GPS.Start(22, 88)
'LastUpdateTime = DateTime.Now
Tracking = True
End Sub
Sub GPS_LocationChanged (Location1 As Location)
Dim strLoc As String
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
ToastMessageShow(Location1.Latitude & "/" & Location1.Longitude,True)
LastLocation=Location1
'GPS.Start(LastLocation.Latitude,LastLocation.Longitude)
LastUpdateTime = DateTime.Now
Else
ToastMessageShow("else 1",True)
End If
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
ToastMessageShow("Service Destroy",True)
If Tracking Then
GPS.Stop
End If
Tracking = False
lock.ReleasePartialLock
End Sub
private Sub RunCounter
Try
Dim IsP_Main As Boolean
IsP_Main=IsPaused(Main)
If IsP_Main Then
counter = counter + 1
If counter = 6 Then
counter=0
'GPS.Start(LastLocation.Latitude,LastLocation.Longitude)
End If
Else
counter = 0
End If
Catch
Log("RunCounter " & LastException)
End Try
End Sub
Sub timer1_tick
Try
RunCounter
'Log("Counter=" & counter & " " & DateUtils.TicksToString(DateTime.Now))
Catch
Log("timer1_tick " & LastException)
End Try
End Sub