Android Question GPS no longer working properly

GaryK4

Member
Licensed User
I developed a golf program some time ago. Suddenly over the last month or two the GPS has become unreliable, almost as the service has died. I have added some debug code which I display on the bottom of the main activity. When it stops working, the first date/time stops updating. I believe it is related to screen off timer, but I have not found a pattern yet. I am not sure what to do next. Recently my Note 8 was updated to Android 9, but I think the problem started before that. android:targetSdkVersion="26".

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim GPS1 As GPS
  
    ' use separate location_changed flag to make sure each activity "sees" the change.
    Type GPS_struct(cur_location As Location, last_update As Long, location_changed As Boolean _
    , map_location_changed As Boolean, Hole_Loc As Location _
    , dist2hole As Double, Str_dist2hole As String, Accuracy As String)
    Dim gps As GPS_struct
  
    Dim timer2 As Timer
  
    Dim TimeDiff As Long

End Sub

Sub Service_Create
    GPS1.Initialize("GPS")
    gps.Initialize
    gps.cur_location.Initialize
    gps.Hole_Loc.Initialize
    gps.Hole_Loc = Main.Hole_Loc  'currently Main.loc gets loaded before here. Init loses the values.
  
    TimeDiff = 0
    timer2.Initialize("Timer2", 500) ' 1000 = 1 second
    timer2.Enabled = True
End Sub

Sub Service_Start (StartingIntent As Intent)
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
        ' minimum read time ms, ninimum location change meters.
        GPS1.Start(500 , .5) 'Listen to GPS with no filters.
    End If
End Sub

Sub Service_Destroy

End Sub

Sub GPS_LocationChanged (inpLoc As Location)
    gps.cur_location = inpLoc
    gps.last_update =  DateTime.now
    gps.location_changed = True
    gps.map_location_changed = True
  
End Sub

Sub Timer2_Tick
    gps.dist2hole = gps.Hole_Loc.DistanceTo(gps.cur_location)
    gps.dist2hole = Round2(gps.dist2hole * 1.09361,0)
    gps.Accuracy = Round2(gps.cur_location.Accuracy,0)
  
    If gps.dist2hole > 4000 Then gps.dist2hole = 0
  
    gps.Str_dist2hole = "D: " & gps.dist2hole &  " - A: " & gps.Accuracy
  
    TimeDiff = Abs(DateTime.now - gps.last_update) / 1000  ' seconds
  
    Dim tmpStr As String
  
    DateTime.DateFormat  = "h:mm:ss a"
    tmpStr = DateTime.Date(DateTime.now)
  
    Main.gpsDebug = tmpStr & " dx: " & TimeDiff
End Sub
 
Last edited:

GaryK4

Member
Licensed User
search for "background location tracking". There is an example of a long running task which gets the location.
Note that you should use a updated SDK, B4a 8+ (better 9+)...
OK, I implemented the code. I have to play a round of golf for the ultimate test. So far, it looks good!
I have updated SDK previously and am running B4a 9.0

Thanks
 
Top