'Sub Process Global
Sub Process_Globals
Private rp As RuntimePermissions
Private gps As GPS
Private timer As Timer
End Sub
'Sub Service Create
Sub Service_Create
gps.Initialize("gps")
timer.Initialize("timer", 60000) ' Intervallo di aggiornamento (60 secondi)
StartServiceAt(Me, DateTime.Now + 60000, True) ' Riavvia il servizio automaticamente
End Sub
'Sub Service Start
Sub Service_Start (StartingIntent As Intent)
If rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION) Then
StartGPS
End If
Service.AutomaticForegroundNotification = CreateNotification("Servizio attivo", "Monitoraggio della posizione")
Service.StartForeground(1, Service.AutomaticForegroundNotification)
End Sub
'Sub GPS Start
Sub StartGPS
If gps.GPSEnabled Then
gps.Start(0, 0) ' Nessun limite di tempo o distanza
timer.Enabled = True
Else
ToastMessageShow("Abilita il GPS", True)
End If
End Sub
'Sub GPS Location Changed
Sub gps_LocationChanged (Location1 As Location)
Log("Lat: " & Location1.Latitude & " Lon: " & Location1.Longitude)
' Puoi inviare la posizione a un server o salvarla in locale
End Sub
'Sub Timer Tick
Sub Timer_Tick
If gps.GPSEnabled Then
gps.Start(0, 0)
Else
ToastMessageShow("GPS Disabilitato", True)
End If
End Sub
'Sub Service Destroy
Sub Service_Destroy
gps.Stop
timer.Enabled = False
StartServiceAt(Me, DateTime.Now + 60000, True) ' Riavvia il servizio automaticamente
End Sub
'Notifica di servizio
Sub CreateNotification(Title As String, Text As String) As Notification
Dim n As Notification
n.Initialize
n.Icon = "icon"
n.SetInfo(Title, Text, Null)
Return n
End Sub
'Sub Boot Receiver per avviare il servizio all'accensione del dispositivo
#Region Receiver: BootReceiver
#If JAVA
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.ServiceHelper;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
BA.LogInfo("BootReceiver: Avvio del servizio GPS");
ServiceHelper.StarterHelper.startServiceFromReceiver(context, null, true);
}
}
}
#End If
#End Region