Android Question GPS doesn't run at the background although service module is used

Baris Karadeniz

Active Member
Licensed User
I used service module for gps. It works at the foreground but it doesn't work at the background. What can be the problem? Service Starter codes are;

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#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 GPS1 As GPS
     Dim n As Notification
     Dim GPS2 As String
     Dim Latitude As String
     Dim Longitude As String
     Dim Speed As String
     Dim Accuracy As String
     Dim Altitude As String
     Dim Heading As String
     Dim Time As String
     Dim lblSatellite As String
     Dim GpsStart As Boolean
    
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    GPS1.Initialize("GPS")
    n.Initialize
    GpsStart = False
End Sub

Sub Service_Start (StartingIntent As Intent)
    StartServiceAt("", DateTime.Now + 5 * DateTime.TicksPerSecond, True)
   
    If GPS1.GPSEnabled = False Then
        GPS2 = "gpsno"
        GPS1.Stop
        GpsStart = False
    Else
        If GpsStart = False Then
        GPS1.Start(5, 10) 'Listen to GPS with no filters.
        GpsStart = True
        End If
        GPS2 = "gpsok"
        If Main.systemon = True Then
        ServerJobs
        End If
    End If
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
    GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    Latitude = NumberFormat(Location1.Latitude, 2, 6)
    Longitude = NumberFormat(Location1.Longitude, 2, 6)
    Speed = NumberFormat(Location1.Speed, 1,0)
    Accuracy = NumberFormat(Location1.Accuracy, 1, 0)
    Altitude = NumberFormat(Location1.Altitude, 1, 1)
    Heading = NumberFormat(Location1.Bearing, 1, 0)
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.TimeFormat = "HH:mm:ss"
    Time = DateTime.Date(Location1.Time) & " " & DateTime.Time(Location1.Time)
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
   
End Sub

Sub GPS_GpsStatus (Satellites As List)
    lblSatellite = "Satellites:" & CRLF
    For i = 0 To Satellites.Size - 1
        Dim Satellite As GPSSatellite
        Satellite = Satellites.Get(i)
        lblSatellite = lblSatellite & CRLF & Satellite.Prn & _
            " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _
            & " " & Satellite.Elevation
    Next
End Sub

Sub ServerJobs
    Dim Job1 As HttpJob
      Job1.Initialize("Job1", Me)
      Job1.Download2("http://www.taksi-m.com.tr/track/server/http/android.php", Array As String("op", GPS2, "imei", Main.imeiService, "dt", Main.TimeService, "lat", Main.LatitudeService, "lng", Main.LongitudeService, "altitude", Main.AltitudeService, "angle", Main.HeadingService, "speed", Main.SpeedService, "params", Main.occupiedService, "event", Main.EventService))
      Main.EventService = ""
    Dim Job2 As HttpJob
       Job2.Initialize("Job2", Me)
       Job2.Download2("http://www.taksi-m.com.tr/track/server/s_service.php", Array As String("op", "chat_new_messages", "imei", Main.imeiService))
End Sub

Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  If Job.Success = True Then
      Select Job.JobName
        Case "Job1"
            'print the result to the logs
            'Log(Job.GetString)
        Case "Job2"
            'print the result to the log
             Log(Job.GetString)
             If Job.GetString > 0 Then   
             n.Icon = "icon"
             n.SetInfo(Main.warningtitle1, Main.AlertMessage, Main)
             n.Notify(1)
             End If
        Case "Job3"
            'print the result to the logs
            'Log(Job.GetString)
      End Select
  Else
      Log("Error: " & Job.ErrorMessage)
  End If
  Job.Release
End Sub
 
Top