GPS in service module

JDS

Active Member
Licensed User
Longtime User
Goodmorning,

At the moment we are using a service module for the GPS.

When using the GPS-example we noticed that the icon of the GPS disappeared when using the back or home button. GPS isn't active any more.
In our application the GPS-icon remains visible after using the back or home button. GPS remains active. That is unwanted behaviour but we can't get it fixed.

To active the GPS this code is used, it stands in a Code Module:

B4X:
Sub CheckGPS
   If ((def_GPSMode=1 AND arZendingen.Size>0) OR def_GPSMode=2 OR (def_GPSMode=0 AND piRijtijd=1)) Then
      GPSService.GPS1.Initialize("GPS")
      StartService(GPSService)
      
      If GPSService.GPS1.GPSEnabled = False Then
         ToastMessageShow("Activeer de GPS.", True)
         StartActivity(GPSService.GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
      Else
         GPSService.GPS1.Start(0, 0) 'Listen to GPS with no filters.
      End If
   End If
End Sub

The service module looks like this:
B4X:
Sub Process_Globals
    Dim GPS1 As GPS : GPS1.Initialize("GPS")
    Dim Currentloc As Location
    Dim Latitude, Longitude As Double
    Dim count As Int
End Sub

Sub Service_Create            
    Currentloc.Initialize() 
End Sub

Sub GPS_LocationChanged (Loc As Location)
    Currentloc = Loc
    count = 0
    count = count + 1
    Longitude = Loc.Longitude
   Latitude = Loc.Latitude 
   Log("in GPS")
   Globaal.GPS_Long    = Currentloc.Longitude
   Globaal.GPS_Long    = Globaal.GPS_Long.Replace(".", ",")
   Globaal.GPS_Lat      = Currentloc.Latitude
   Globaal.GPS_Lat    = Globaal.GPS_Lat.Replace(".", ",")
   Globaal.GPS_Alt    = Currentloc.Altitude
   Globaal.GPS_Alt    = Globaal.GPS_Alt.Replace(".", ",")
   Globaal.GPS_Speed    = Currentloc.Speed
   If (Globaal.lastGPSfile < (DateTime.Now - Globaal.GPS_Interval * DateTime.TicksPerSecond)) Then
      Globaal.lastGPSfile = DateTime.Now   
      createRecord
   End If
   GPS1.Stop 
   
End Sub

Sub Service_Start (StartingIntent As Intent)
    GPS1.Start(0, 0)
End Sub

Sub Service_Destroy
    Log("GPS service destroyed")
    GPS1.Stop
End Sub


Sub createRecord
    Dim lat, lon, alt As String
   Dim lNew, lNow, lOld As Long
   Dim sSpeed, sGPS, sTime As String
    
   If ((Globaal.def_GPSMode=1 AND Globaal.arZendingen.Size>0) OR Globaal.def_GPSMode=2 OR Globaal.def_GPSMode=0 AND Globaal.piRijtijd=1) Then
      Globaal.GPS_LastTime = DateTime.Now
      lNow = DateTime.Now
      sTime = DateTime.Time(lNow)
      lNew = DateTime.TimeParse(sTime)
      sGPS = "<gps>" & Globaal.getdate & "|" & Globaal.getTimeNoLimiter & "|" & Globaal.gps_Alt & "|" & Globaal.gps_long & "|" & Globaal.GPS_Alt & "|" & Globaal.GPS_Speed &"</gps>" 
      File.WriteString(Globaal.Map_GPS,  "GPS" & Globaal.getDateNoLimiter & Globaal.getTimeNoLimiter & ".xml", sGPS)
   End If
   GPS1.Stop
   Globaal.PutGPS
End Sub

The pauze event in the activity is:
B4X:
Sub Activity_Pause (UserClosed As Boolean)
   Log("Pauze Main "&UserClosed )
   'CallSub(GPSService,"Stop_Service")
   GPSService.GPS1.Stop
   StopService(GPSService)
   StopService(HttpUtils2Service)
   If UserClosed Then
      Activity.Finish
   End If
End Sub

The question is how to stop the GPS and let the GPS-icon disapeare. When logging the Service_Destroy of the Service is being called.
 
Top