Italian Geolocalizzazione in background

Fulvio75

Well-Known Member
Licensed User
Ciao a tutti ho visto degli esempi di codice sulla geolocalizzazione in background, qualcuno ne ha utilizzato uno funzionante e se si quale?
Il mio scopo è di integrare un servizio che si avvia all'avvio del telefono in una app già esistente per ottenere la posizione anche senza aprire appunto l'applicazione stessa che contiene questo servizio
 

Fulvio75

Well-Known Member
Licensed User
Non va...

Chatgpt dice questo funzionerà?
B4X:
'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
 

Sagenut

Expert
Licensed User
Longtime User
L'app la puoi fare partire con il BOOT_COMPLETED
Altrimenti forse la soluzione migliore è fare un widget.
Quello dovrebbe essere subito attivo al riavvio del telefono.
 

Fulvio75

Well-Known Member
Licensed User
L'app la puoi fare partire con il BOOT_COMPLETED
Altrimenti forse la soluzione migliore è fare un widget.
Quello dovrebbe essere subito attivo al riavvio del telefono.
I widget si ne ho parecchie dovrei fare un widget vuoto
 
Top