Background service to report GPS position

neavilag

Member
Licensed User
Longtime User
Hi, I have just got into B4A and think is great product, I already saw how services works with the Twitter Search Sample. I and wondering to use the same approach to report gps data (lat, lon,speed,bearing) background to a website on the same approach. I already do this with an AppInventor app but it only works when on foreground.

My question is, what is the best approach, to make it report, as a service?

Turn on and Turn off gps each time (for example every 3 mins) as the twitter sample.. ?

will this wont be kind of slow to get a fix every time ?

Thanks for any guidance.

regards
 

neavilag

Member
Licensed User
Longtime User
Thanks Erel, I don´t know if I did it right, did this but seems is not working as desired.

1. Why do I have an Activity ? just to call the service ?

2. seems like the service start, and report a accurate position if after some time did not got a accurate position, stop gps and wait for next round to try to get a fix.. But seems that it is not working.

Only first time that get a good reading will stop gps, after it will not..

3. How I can make to start service only every time phone starts ?


see code below, hope you give me better guidance

thanks

Norman

B4X:
'Service module
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 count As Int

End Sub
Sub Service_Create
GPS1.Initialize("GPS")
 If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    End If

End Sub

Sub GPS_LocationChanged (Location1 As Location)
   count = count + 1
   If Location1.Accuracy <=100 Then
      'call sub to send data to server
      ToastMessageShow("GPS Data Acc" & Location1.Accuracy & " " & Location1.Latitude & " " & Location1.Longitude, True)
      GPS1.Stop 
   End If
   
   If count >15 Then
      ToastMessageShow("No Accurate GPS Signal... Aborting Report", True)
      GPS1.Stop
   End If
   
   If count Mod 5 = 0 Then
      ToastMessageShow("Locating..." & count, True)
   End If
   
End Sub

Sub Service_Start (StartingIntent As Intent)
      StartServiceAt("", DateTime.Now + 2 * DateTime.TicksPerMinute, False)
     ToastMessageShow("SS Turning GPS ON", True)
      GPS1.Start(0, 0) 'Listen to GPS with no filters.
     count = 0
End Sub

Sub Service_Destroy

End Sub

Main Activity code only starts service.. black screen.. :-/

B4X:
Sub Activity_Create(FirstTime As Boolean)
  ToastMessageShow("Starting Service", True)
  StartService(ecuservice)
End Sub
 
Upvote 0
Top