Android Question FusedLocationProvider slow v13(33)

AHilberink

Active Member
Licensed User
Longtime User
Hi,

I use FusedLocationProvider for my app for some time without problems. Now I am experiencing some delay on the LocationChanged-event with android 13(33). My client did buy new tablets and now I have this problem. It looks like a slow event or no event at all.

Did anyone experience problems with the latest versions of android?

I use this service:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim LatLonTrace As Location
    Dim LatLonRoute As Location
    Dim LatLonKm As Location
    Dim GPSTrack1 As FusedLocationProvider
    Dim Route As List
    Dim RouteTijd As List
    Dim OpnemenRoute As Boolean
    Dim GeredenKm As Float
    Dim LastKnownLocation As Location
End Sub

Sub Service_Create
    If(LatLonTrace.IsInitialized=False) Then LatLonTrace.Initialize
    If(LatLonRoute.IsInitialized=False) Then LatLonRoute.Initialize
    If(LatLonKm.IsInitialized=False) Then LatLonKm.Initialize
    If(Route.IsInitialized=False) Then Route.Initialize
    If(RouteTijd.IsInitialized=False) Then RouteTijd.Initialize
    If(LastKnownLocation.IsInitialized=False) Then LastKnownLocation.Initialize
    If(GPSTrack1.IsInitialized=False) Then GPSTrack1.Initialize("GPSTrack")
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_NEVER
    Starter.Lock.PartialLock
    'If((Starter.GPSLoc Or Starter.GPS) And LastKnownLocation.Longitude<>0) Then SendLatLon(LastKnownLocation)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(999,RemoveIcon)
    StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, True)
    If(Starter.GPSLoc Or Starter.GPS) Then Track
    Log("Heartbeat: "&DateTime.Time(DateTime.Now))
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy
    Log("GPSTrack Destroyed")
    If GPSTrack1.IsConnected Then
        GPSTrack1.Disconnect
        Route.Clear
        RouteTijd.Clear
    End If
    Starter.Lock.ReleasePartialLock
    StartService(Me)
End Sub

Public Sub Track
    If (GPSTrack1.IsConnected) Then
        Return
    End If
    If (Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)=False) Then
        Log("No permission")
        Return
    Else
        GPSTrack1.Connect
    End If
End Sub

Sub GPSTrack_ConnectionSuccess
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(5000)         '1000 is 1 seconde
    LocationRequest1.SetFastestInterval(LocationRequest1.GetInterval / 2)
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_BALANCED_POWER_ACCURACY) '.PRIORITY_HIGH_ACCURACY)
    Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
    LocationSettingsRequestBuilder1.Initialize
    LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
    GPSTrack1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)
    GPSTrack1.RequestLocationUpdates(LocationRequest1)
    If(Starter.GPSFromSuspend) Then
        Starter.GPSFromSuspend=False
        'Starter.ZendError("module=GPSLocatie&error=ConnectionSuccess")
    End If
End Sub

Sub GPSTrack_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
End Sub

Sub  GPSTrack_ConnectionFailed(ConnectionResult1 As Int)
    Log("ConnectionFailed")
    'Starter.ZendError("module=GPSLocatie&error=Destroyed by ConnectionFailed. Result: "&ConnectionResult1)
    Starter.GPSFromSuspend=True
    Starter.HerstartGPS
End Sub

Sub GPSTrack_ConnectionSuspended(SuspendedCause1 As Int)
    Dim Cause As String
    
    Log("ConnectionSuspended")
    If(SuspendedCause1=GPSTrack1.SuspendedCause.CAUSE_NETWORK_LOST) Then
        Cause="Suspended by NetworkLost"
    Else If(SuspendedCause1=GPSTrack1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED) Then
        Cause="Suspended by Disconnected"
    Else
        Cause="Suspended: Unknow cause"
    End If
    DateTime.DateFormat="dd-MM-yyyyy"
    Cause=Cause&": "&DateTime.date(DateTime.Now)&"_"&DateTime.Time(DateTime.Now)
    Log(Cause)
    'Starter.ZendError("module=GPSLocatie&error="&Cause)
    Starter.GPSFromSuspend=True
    Starter.HerstartGPS
End Sub

Sub RemoveIcon As Notification
    Dim ic As Notification
    
    ic.Initialize2(ic.IMPORTANCE_LOW)
    ic.Icon="empty"
    ic.Sound=False
    ic.SetInfo("","",Null)
    Return ic
End Sub

Sub GPSTrack_LocationChanged(Location1 As Location)
    Dim DatumTijd As String
    
    'Log("CheckLocationChanged: "&DateTime.Time(DateTime.Now))
    'Log("Afstand: "&Location1.DistanceTo(LatLonTrace))
    LastKnownLocation=Location1

    If(Starter.GPSLoc) Then
        If(Location1.DistanceTo(LatLonTrace)/1000>0.1) Then '100m
            LatLonTrace=Location1
            SendLatLon(Location1)
        End If
    End If

    If(OpnemenRoute) Then
        If(Route.IsInitialized=False) Then Route.Initialize
        If(RouteTijd.IsInitialized=False) Then RouteTijd.Initialize
        If(Location1.DistanceTo(LatLonRoute)/1000>0.5) Then '500m
            DateTime.DateFormat="dd-MM-yyyy"
            DatumTijd=DateTime.Date(DateTime.Now)&" "&DateTime.Time(DateTime.Now)
            Route.Add(Location1)
            RouteTijd.Add(DatumTijd)
            LatLonRoute=Location1
        End If
    End If

    If(Hoofdscherm.HuidigeCirisID<>0) Then
        If(LatLonKm.IsInitialized=False) Then LatLonKm.Initialize
        GeredenKm=GeredenKm+Location1.DistanceTo(LatLonKm)/1000
        LatLonKm=Location1
        
        For i=0 To Starter.Vestigingen.Size-1
            Dim Kantoor As Location
            Kantoor.Initialize
            Kantoor=Starter.Vestigingen.GetValueAt(i)
            If(Kantoor.Latitude<>0 And EigenFuncties.GetDistance(Location1,Kantoor)<=2 And EigenFuncties.GetDistance(Location1,Kantoor)<>0) Then
                Hoofdscherm.ActiveWeegbrug=Starter.Vestigingen.GetKeyAt(i)
                CallSub2(Hoofdscherm,"WeegStatus",Starter.VestigingNamen.GetValueAt(i))
                Exit
            Else
                CallSub2(Hoofdscherm,"WeegStatus","")
            End If
        Next
    End If
End Sub

Sub SendLatLon(Location1 As Location)
    Dim HTTPLocatie As HttpJob
    
    HTTPLocatie.Initialize("",Me)
    HTTPLocatie.Download(Main.ApiUrl&"SendLatLon"&Starter.ParameterVast&"&user="&Starter.Gebruiker&"&lat="&Location1.Latitude&"&lon="&Location1.Longitude&"&pauze=0")
    Wait For (HTTPLocatie) JobDone(HTTPLocatie As HttpJob)
    If(HTTPLocatie.Success = True) Then
    Else
    End If
    HTTPLocatie.Release
End Sub

I hope someone can help me.

Kind regards,
André
 

Alex_197

Well-Known Member
Licensed User
Longtime User
Hi,

I use FusedLocationProvider for my app for some time without problems. Now I am experiencing some delay on the LocationChanged-event with android 13(33). My client did buy new tablets and now I have this problem. It looks like a slow event or no event at all.

Did anyone experience problems with the latest versions of android?

I use this service:
B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim LatLonTrace As Location
    Dim LatLonRoute As Location
    Dim LatLonKm As Location
    Dim GPSTrack1 As FusedLocationProvider
    Dim Route As List
    Dim RouteTijd As List
    Dim OpnemenRoute As Boolean
    Dim GeredenKm As Float
    Dim LastKnownLocation As Location
End Sub

Sub Service_Create
    If(LatLonTrace.IsInitialized=False) Then LatLonTrace.Initialize
    If(LatLonRoute.IsInitialized=False) Then LatLonRoute.Initialize
    If(LatLonKm.IsInitialized=False) Then LatLonKm.Initialize
    If(Route.IsInitialized=False) Then Route.Initialize
    If(RouteTijd.IsInitialized=False) Then RouteTijd.Initialize
    If(LastKnownLocation.IsInitialized=False) Then LastKnownLocation.Initialize
    If(GPSTrack1.IsInitialized=False) Then GPSTrack1.Initialize("GPSTrack")
    Service.AutomaticForegroundMode=Service.AUTOMATIC_FOREGROUND_NEVER
    Starter.Lock.PartialLock
    'If((Starter.GPSLoc Or Starter.GPS) And LastKnownLocation.Longitude<>0) Then SendLatLon(LastKnownLocation)
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(999,RemoveIcon)
    StartServiceAt(Me, DateTime.Now + 15 * DateTime.TicksPerMinute, True)
    If(Starter.GPSLoc Or Starter.GPS) Then Track
    Log("Heartbeat: "&DateTime.Time(DateTime.Now))
End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy
    Log("GPSTrack Destroyed")
    If GPSTrack1.IsConnected Then
        GPSTrack1.Disconnect
        Route.Clear
        RouteTijd.Clear
    End If
    Starter.Lock.ReleasePartialLock
    StartService(Me)
End Sub

Public Sub Track
    If (GPSTrack1.IsConnected) Then
        Return
    End If
    If (Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)=False) Then
        Log("No permission")
        Return
    Else
        GPSTrack1.Connect
    End If
End Sub

Sub GPSTrack_ConnectionSuccess
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(5000)         '1000 is 1 seconde
    LocationRequest1.SetFastestInterval(LocationRequest1.GetInterval / 2)
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_BALANCED_POWER_ACCURACY) '.PRIORITY_HIGH_ACCURACY)
    Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
    LocationSettingsRequestBuilder1.Initialize
    LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
    GPSTrack1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)
    GPSTrack1.RequestLocationUpdates(LocationRequest1)
    If(Starter.GPSFromSuspend) Then
        Starter.GPSFromSuspend=False
        'Starter.ZendError("module=GPSLocatie&error=ConnectionSuccess")
    End If
End Sub

Sub GPSTrack_LocationSettingsChecked(LocationSettingsResult1 As LocationSettingsResult)
End Sub

Sub  GPSTrack_ConnectionFailed(ConnectionResult1 As Int)
    Log("ConnectionFailed")
    'Starter.ZendError("module=GPSLocatie&error=Destroyed by ConnectionFailed. Result: "&ConnectionResult1)
    Starter.GPSFromSuspend=True
    Starter.HerstartGPS
End Sub

Sub GPSTrack_ConnectionSuspended(SuspendedCause1 As Int)
    Dim Cause As String
   
    Log("ConnectionSuspended")
    If(SuspendedCause1=GPSTrack1.SuspendedCause.CAUSE_NETWORK_LOST) Then
        Cause="Suspended by NetworkLost"
    Else If(SuspendedCause1=GPSTrack1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED) Then
        Cause="Suspended by Disconnected"
    Else
        Cause="Suspended: Unknow cause"
    End If
    DateTime.DateFormat="dd-MM-yyyyy"
    Cause=Cause&": "&DateTime.date(DateTime.Now)&"_"&DateTime.Time(DateTime.Now)
    Log(Cause)
    'Starter.ZendError("module=GPSLocatie&error="&Cause)
    Starter.GPSFromSuspend=True
    Starter.HerstartGPS
End Sub

Sub RemoveIcon As Notification
    Dim ic As Notification
   
    ic.Initialize2(ic.IMPORTANCE_LOW)
    ic.Icon="empty"
    ic.Sound=False
    ic.SetInfo("","",Null)
    Return ic
End Sub

Sub GPSTrack_LocationChanged(Location1 As Location)
    Dim DatumTijd As String
   
    'Log("CheckLocationChanged: "&DateTime.Time(DateTime.Now))
    'Log("Afstand: "&Location1.DistanceTo(LatLonTrace))
    LastKnownLocation=Location1

    If(Starter.GPSLoc) Then
        If(Location1.DistanceTo(LatLonTrace)/1000>0.1) Then '100m
            LatLonTrace=Location1
            SendLatLon(Location1)
        End If
    End If

    If(OpnemenRoute) Then
        If(Route.IsInitialized=False) Then Route.Initialize
        If(RouteTijd.IsInitialized=False) Then RouteTijd.Initialize
        If(Location1.DistanceTo(LatLonRoute)/1000>0.5) Then '500m
            DateTime.DateFormat="dd-MM-yyyy"
            DatumTijd=DateTime.Date(DateTime.Now)&" "&DateTime.Time(DateTime.Now)
            Route.Add(Location1)
            RouteTijd.Add(DatumTijd)
            LatLonRoute=Location1
        End If
    End If

    If(Hoofdscherm.HuidigeCirisID<>0) Then
        If(LatLonKm.IsInitialized=False) Then LatLonKm.Initialize
        GeredenKm=GeredenKm+Location1.DistanceTo(LatLonKm)/1000
        LatLonKm=Location1
       
        For i=0 To Starter.Vestigingen.Size-1
            Dim Kantoor As Location
            Kantoor.Initialize
            Kantoor=Starter.Vestigingen.GetValueAt(i)
            If(Kantoor.Latitude<>0 And EigenFuncties.GetDistance(Location1,Kantoor)<=2 And EigenFuncties.GetDistance(Location1,Kantoor)<>0) Then
                Hoofdscherm.ActiveWeegbrug=Starter.Vestigingen.GetKeyAt(i)
                CallSub2(Hoofdscherm,"WeegStatus",Starter.VestigingNamen.GetValueAt(i))
                Exit
            Else
                CallSub2(Hoofdscherm,"WeegStatus","")
            End If
        Next
    End If
End Sub

Sub SendLatLon(Location1 As Location)
    Dim HTTPLocatie As HttpJob
   
    HTTPLocatie.Initialize("",Me)
    HTTPLocatie.Download(Main.ApiUrl&"SendLatLon"&Starter.ParameterVast&"&user="&Starter.Gebruiker&"&lat="&Location1.Latitude&"&lon="&Location1.Longitude&"&pauze=0")
    Wait For (HTTPLocatie) JobDone(HTTPLocatie As HttpJob)
    If(HTTPLocatie.Success = True) Then
    Else
    End If
    HTTPLocatie.Release
End Sub

I hope someone can help me.

Kind regards,
André
maybe it's a problem with the tablet location? Bad connection like in the basement?
 
Upvote 0

Ivica Golubovic

Active Member
Licensed User
You are using an outdated fused location library that was adapted for an older version of the Google Play service. Download the newer FusedLocationProviderGMS library, customize the program and it will work without problems.
 
Upvote 0
Top