Sub Process_Globals
Private rp As RuntimePermissions
Private gps As GPS
Private timer As Timer
End Sub
Sub Service_Create
gps.Initialize("gps")
timer.Initialize("timer", 60000)
StartServiceAt(Me, DateTime.Now + 60000, True)
End Sub
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 StartGPS
If gps.GPSEnabled Then
gps.Start(0, 0)
timer.Enabled = True
Else
ToastMessageShow("Abilita il GPS", True)
End If
End Sub
Sub gps_LocationChanged (Location1 As Location)
Log("Lat: " & Location1.Latitude & " Lon: " & Location1.Longitude)
End Sub
Sub Timer_Tick
If gps.GPSEnabled Then
gps.Start(0, 0)
Else
ToastMessageShow("GPS Disabilitato", True)
End If
End Sub
Sub Service_Destroy
gps.Stop
timer.Enabled = False
StartServiceAt(Me, DateTime.Now + 60000, True)
End Sub
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
#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