Android Question Background location tracking problem

Vicente Barba

Member
Licensed User
Hi everyone,

I have a problem with an app already published on Google Play.

It is a WebView that interacts with the rest of the code, intercepting the url with webView_OverrideUrl.
If you click on a link like "<a href='gpson:myusername:www.myserver.com/result'>" the GPS is activated and the app reports to a server.
When the server decides, sends a message to stop the GPS.

The problem is that phones have gone into a loop, more than 30 times per second, and they contact the server to report the GPS.

In the manifest it is indicated:

Manifest:
SetServiceAttribute(Alarm, android:foregroundServiceType, "location")

This is the code for the main module:

Main:
Sub webView_OverrideUrl (url As String) As Boolean
    'url type: "gpson:user:redirect"
    If url.StartsWith("gpson") Then
        urlGpsOn(url)
        Return True
    End If    
    Return False
End Sub


Sub urlGpsOn(url As String)
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Dim components() As String
        components = Regex.Split(":", url)
        Dim user As String = components(1).ToUpperCase
        Dim redirect As String = "https://" & components(2)
        DB.setConfigValue("user", user)
        StartService(Alarm)
        ToastMessageShow("GPS Tracking ON", True)
        webView.LoadUrl(redirect)
    Else
        ToastMessageShow("No GPS Tracking", True)
    End If
End Sub

And the service:

Service:
#Region  Service Attributes 
    #StartAtBoot: True
#End Region

Sub Process_Globals
    Private GPS As GPS
    Private Tracking As Boolean
    Private LastUpdateTime As Long
    Private lock As PhoneWakeState
    Private nid As Int = 1
End Sub

Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
    GPS.Initialize("gps")
    lock.PartialLock
End Sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("GPS Tracking", "..."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
    Track
End Sub

Public Sub Track
    If Tracking Then Return
    If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
        Return
    End If
    GPS.Start(0, 0)
    Tracking = True
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    If DateTime.Now > LastUpdateTime + 300 * DateTime.TicksPerSecond Then

        'Ping to server to stop tracking if necesary
        Dim Job As HttpJob
        Job.Initialize("ALARM", Me)
        Job.Username = Main.webus
        Job.Password = Main.webpw
        Dim user As String = DB.getConfigValue("user")
        Job.Download2(Main.urlPing, Array As String("usr", user, "app", Main.appVersion))
        Wait For (Job) JobDone(Job As HttpJob)
        If Job.Success Then
            If Job.GetString = Main.pingGPSOff Then
                Service_Destroy
                Dim n As Notification = CreateNotification("No GPS Tracking", "Service disabled")
                n.Notify(nid)
                Return
            End If
        End If
        Job.Release
                
        'Update notification
        Dim n As Notification = CreateNotification("GPS Tracking", $"$2.5{Location1.Latitude} / $2.5{Location1.Longitude}"$)
        n.Notify(nid)
        LastUpdateTime = DateTime.Now
        
        'Save GPS data
        Dim reported As Int = 0
        Dim date_time As Long = Helper.getDateAsLong
        Dim latitude As Double = Location1.Latitude
        Dim longitude As Double = Location1.Longitude
        DB.sql.ExecNonQuery2("INSERT INTO tracking (user, reported, date_time, latitude, longitude) VALUES (?, ?, ?, ?, ?)", Array As Object(user, reported, date_time, latitude, longitude))
        
        'Send GPS data to server
        Dim rs As ResultSet
        rs = DB.getTracking
        Do While rs.NextRow
            Dim Job As HttpJob
            Job.Initialize("ALARM", Me)
            Job.Username = Main.webus
            Job.Password = Main.webpw
            date_time = rs.GetLong("date_time")
            latitude = rs.GetDouble("latitude")
            longitude = rs.GetDouble("longitude")
            Dim data As String
            data = "usr=" & user & "&dat=" & date_time & "&lat=" & latitude & "&lon=" & longitude
            Job.PostString(Main.urlTracking, data)
            Wait For (Job) JobDone(Job As HttpJob)
            If Job.Success Then
                DB.deleteTracking(rs.GetLong("id_tracking"))
            End If
            Job.Release
        Loop

    End If
End Sub

Sub CreateNotification(Title As String, Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo(Title, Body, Main)
    Return notification
End Sub

Sub Service_Destroy
    If Tracking Then
        GPS.Stop
    End If
    Tracking = False
    lock.ReleasePartialLock
End Sub
 
Top