mirror of
https://github.com/cheveguerra/FLP_2.0.git
synced 2026-04-17 19:36:42 +00:00
Commit inicial
This commit is contained in:
180
FLP.bas
Normal file
180
FLP.bas
Normal file
@@ -0,0 +1,180 @@
|
||||
B4A=true
|
||||
Group=Default Group
|
||||
ModulesStructureVersion=1
|
||||
Type=Service
|
||||
Version=10.5
|
||||
@EndOfDesignText@
|
||||
#Region Service Attributes
|
||||
#StartAtBoot: True
|
||||
#End Region
|
||||
'******************************************************************************
|
||||
'No olvidar agregar esta linea al editor de manifiesto:
|
||||
' SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
|
||||
'
|
||||
'En Starter agregar estas lineas en Process_Globals
|
||||
' Public rp As RuntimePermissions
|
||||
' Public FLP As FusedLocationProvider
|
||||
' Private flpStarted As Boolean
|
||||
'
|
||||
'En Main agregar estas lineas a Activity_Resume
|
||||
' Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
|
||||
' Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
|
||||
' If Result Then
|
||||
' StartService(Tracker)
|
||||
' Log("Start Tracker")
|
||||
' Else
|
||||
' ToastMessageShow("No permission", True)
|
||||
' End If
|
||||
'
|
||||
'Se necesita la libreria FusedLocationProvider, Phone, GPS y RunTimePermissions
|
||||
'
|
||||
'Y en Main agregar estas dos lineas:
|
||||
'#AdditionalJar: com.android.support:support-v4
|
||||
'#AdditionalJar: com.google.android.gms:play-services-location
|
||||
|
||||
Sub Process_Globals
|
||||
Private nid As Int = 1
|
||||
'Private GPS As GPS
|
||||
Private Tracking As Boolean
|
||||
Private LastUpdateTime As Long
|
||||
Private lock As PhoneWakeState
|
||||
'Para FusedLocationProvider (2 lineas)
|
||||
Public FLP As FusedLocationProvider
|
||||
Private flpStarted As Boolean
|
||||
End Sub
|
||||
|
||||
Sub Service_Create
|
||||
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
|
||||
'Para FusedLocationProvider (2 lineas)
|
||||
FLP.Initialize("flp")
|
||||
FLP.Connect
|
||||
lock.PartialLock
|
||||
StartFLP
|
||||
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)
|
||||
'Para FusedLocationProvider (1 linea)
|
||||
Service.StopAutomaticForeground
|
||||
Service.StartForeground(nid, CreateNotification("..."))
|
||||
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
|
||||
Track
|
||||
End Sub
|
||||
|
||||
Public Sub StartFLP
|
||||
Log("StartFLP - flpStarted="&flpStarted)
|
||||
Do While FLP.IsConnected = False
|
||||
Sleep(500)
|
||||
Log("sleeping")
|
||||
Loop
|
||||
If flpStarted = False Then
|
||||
Log("RequestLocationUpdates")
|
||||
FLP.RequestLocationUpdates(CreateLocationRequest) 'Buscamos ubicacion
|
||||
Log("Buscamos ubicacion")
|
||||
flpStarted = True
|
||||
Else
|
||||
FLP.RequestLocationUpdates(CreateLocationRequest2times) 'Buscamos ubicacion 2 peticiones
|
||||
Log("Buscamos ubicacion 2 peticiones")
|
||||
End If
|
||||
|
||||
End Sub
|
||||
|
||||
Private Sub CreateLocationRequest As LocationRequest
|
||||
'Log("CreateLocationRequest")
|
||||
Dim lr As LocationRequest
|
||||
lr.Initialize
|
||||
lr.SetInterval(10000)
|
||||
lr.SetFastestInterval(lr.GetInterval / 2)
|
||||
lr.SetSmallestDisplacement(10) 'Solo registra cambio de ubicacion si es mayor a 50 mts
|
||||
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
||||
Return lr
|
||||
End Sub
|
||||
|
||||
Private Sub CreateLocationRequest2times As LocationRequest
|
||||
'Log("CreateLocationRequest")
|
||||
Dim lr As LocationRequest
|
||||
lr.Initialize
|
||||
lr.SetInterval(5000)
|
||||
lr.SetFastestInterval(lr.GetInterval / 2)
|
||||
lr.setNumUpdates(2)
|
||||
lr.SetSmallestDisplacement(50) 'Solo registra cambio de ubicacion si es mayor a 50 mts
|
||||
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
|
||||
Return lr
|
||||
End Sub
|
||||
|
||||
Public Sub StopFLP
|
||||
'Log("StopFLP")
|
||||
If flpStarted Then
|
||||
FLP.RemoveLocationUpdates
|
||||
flpStarted = False
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Public Sub Track
|
||||
'Log("Track")
|
||||
If Tracking Then Return
|
||||
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
|
||||
Log("No permission")
|
||||
Return
|
||||
End If
|
||||
'Para FusedLocationProvider (1 linea)
|
||||
StartFLP
|
||||
Tracking = True
|
||||
End Sub
|
||||
|
||||
Sub flp_LocationChanged (Location1 As Location)
|
||||
If DateTime.Now > LastUpdateTime + 10 * DateTime.TicksPerSecond Then
|
||||
Dim n As Notification = CreateNotification($"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
|
||||
n.Notify(nid)
|
||||
LastUpdateTime = DateTime.Now
|
||||
End If
|
||||
Log("loc changed")
|
||||
Dim coords As String = Location1.Latitude&","&Location1.Longitude&","&Main.devModel
|
||||
CallSub2(Main,"ubicacionRecibida",coords)
|
||||
'Cambiamos el formato de la hora
|
||||
Dim OrigFormat As String=DateTime.DateFormat 'save orig date format
|
||||
DateTime.DateFormat="MMM-dd HH:mm:ss"
|
||||
Dim lastUpdate As String=DateTime.Date(DateTime.Now)
|
||||
DateTime.DateFormat=OrigFormat 'return to orig date format
|
||||
|
||||
'Escribimos coordenadas y fecha a un archivo de texto
|
||||
' Dim ubic As String = Location1.Latitude&","&Location1.Longitude&"|"&lastUpdate
|
||||
' Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "gps.txt", True)
|
||||
' Dim s As String = ubic & CRLF
|
||||
' Dim t() As Byte = s.GetBytes("UTF-8")
|
||||
' out.WriteBytes(t, 0, t.Length)
|
||||
' out.Close
|
||||
|
||||
Log("Loc changed : "&Location1.Latitude&","&Location1.Longitude&"|"&Location1.Accuracy&"|"&lastUpdate)
|
||||
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 Tracking Then
|
||||
StopFLP
|
||||
End If
|
||||
Tracking = False
|
||||
lock.ReleasePartialLock
|
||||
End Sub
|
||||
|
||||
'//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
'//////////////////////////////// EXTRAS ///////////////////////////////////////////////////////
|
||||
'//////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
Public Sub StartLocationUpdates
|
||||
FLP.RequestLocationUpdates(CreateLocationRequest)
|
||||
End Sub
|
||||
Reference in New Issue
Block a user