Possible to poll GPS as service and post http

straybullet

Member
Licensed User
Longtime User
I want to make an app that polls the gps every 5 minutes, then post the long & lat cords to a url of my choosing, I want it to run as a background service on my phone and not bother me with any screens, it just runs on phone start and stays running, loops every 5 mins.

Making a kind of phone tracking device so the next time I loose my phone or leave it in my kids/wife's car I know its not gone for good.


Is this possible with B4A currently?
 

Roger Garstang

Well-Known Member
Licensed User
Longtime User
Some paid for services/apps on carriers or mfg sites do this already, but always good to make your own free. May want to add a button on the web side to set an alarm, so next push of coords will send a string back to the device telling the app to play a sound. Then if nearby you can find it...or if stolen you can annoy them and find it.
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
I've done the same thing for myself but when my phone screen goes blank, the GPS stops working or my Service stops working, I don't know which but I don't get my lat/long emails anymore.

I'm using StartServiceAt with True flag set for working DuringSleep. I'm puzzled as to why it stops working when the screen goes blank.

Edit: I just recalled that I press the off button to turn my phone off and that's when it stops working (in the Service module making its FTP connection to download the small text file and email me). Does turning off the phone stop the Service module and the StartServiceAt() to also stop?
 
Last edited:
Upvote 0

straybullet

Member
Licensed User
Longtime User
Wow where to begin, I never made a service before so is the code structure that different from a normal app?

Besides telling the service to call it's self ever so often is there anything else that goes in the service subs or everything under normal processes such as

Activity_Create

Thanks
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
Ok I am making it as a normal app then moving it to a service so I can test it... But Everything works using various examples on the forum, but it will not actually get the url that is created.


The Php scipt I created on the server works fine I can manually enter it in a web browser and it records it to a text file on the server, but my B4A app never seems to pull the URL.

The toast FURL variable is correct, what am I missing to get it to fire off the httpclient. I do not have any webviews loaded, but do not think I need them for this. I have the phone,core,gps,http and network libs all loaded.

Thanks,

B4X:
'Activity 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 DeviceID1 As PhoneId
Dim count As Int
Dim HttpClient1 As HttpClient
Dim request As HttpRequest


End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
 
  GPS1.Initialize("GPS")
  GPS1.Start(0, 0) 'Listen to GPS with no filters.
  count = 0
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GPS_LocationChanged (Location1 As Location)
    Dim DevID As String 
    Dim Device As Phone
   Dim URL As String   
   Dim FURL As String
   URL = "http://www.mywebsite.com/myscript/do.php"
    count = count + 1
   DevID = DeviceID1.GetDeviceId
    
   If Location1.Accuracy <=100 Then
        'call sub to send data to server
        'ToastMessageShow("GPS Data Acc " & Location1.Accuracy & " " & Location1.Latitude & " " & Location1.Longitude & "DID " & DevID, True)
   
      FURL = URL & "?lat=" & Location1.Latitude & "&lon=" & Location1.Longitude & "&did=" & DevID
      ToastMessageShow(FURL, True)
      'request.InitializeGet(URL & "?lat=" & Location1.Latitude & "&lon=" & Location1.Longitude & "&did=" & DevID)
      request.InitializeGet(FURL)
      request.Timeout = 10000 'set timeout to 10 seconds
       HttpClient1.Execute(request, 1)
       GPS1.Stop 
    End If
    
    If count >15 Then
        ToastMessageShow("No Accurate GPS Signal... Aborting", True)
        GPS1.Stop
    End If
    
    If count Mod 5 = 0 Then
       ToastMessageShow("Locating..." & count, True)
    End If
    
End Sub
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim resultString As String
    Result = Response.GetString("UTF8")
    'Work with the result
End Sub
Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Log("Error connecting: " & Reason & " " & StatusCode)
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
     End If
End Sub
 
Upvote 0

straybullet

Member
Licensed User
Longtime User
Actually I did not initalize the httpclient


But there is another issue, I got it all working as a service, but when I went inside a building , the location icon stayed solid and when it got signal again it like send hundreds if updates to the web sites, seconds apart, but it is set to run the service every 5 minutes.

Trying to find a way to keep this from happening... Here is my service code if you have any suggestions I appreciate it


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 DeviceID1 As PhoneId
Dim count As Int
Dim HttpClient1 As HttpClient
Dim request As HttpRequest


End Sub
Sub Service_Create
  GPS1.Initialize("GPS")
  HttpClient1.Initialize("HttpClient1")
End Sub

Sub Service_Start (StartingIntent As Intent)
  StartServiceAt("", DateTime.Now + 5 * DateTime.TicksPerMinute, False)
  GPS1.Stop
  GPS1.Start(0, 0) 'Listen to GPS with no filters.
  count = 0
  End Sub

Sub Service_Destroy

End Sub

Sub GPS_LocationChanged (Location1 As Location)
    Dim DevID As String 
    Dim Device As Phone
   Dim URL As String   
   Dim FURL As String
   Dim server As ServerSocket 'Add a reference to the network library
    server.Initialize(0, "")
    If server.GetMyIP = "127.0.0.1" Then Return
   URL = "http://www.mywebsite.com/myscript/do.php"
    count = count + 1
   DevID = DeviceID1.GetDeviceId

   If count >15 Then
    GPS1.Stop
    Return
   End If
   
   
   If Location1.Accuracy <=100 Then        
      FURL = URL & "?lat=" & Location1.Latitude & "&lon=" & Location1.Longitude & "&did=" & DevID
      'ToastMessageShow(FURL, True)
      request.InitializeGet(FURL)
      request.Timeout = 10000
       HttpClient1.Execute(request, 1)
       GPS1.Stop 
    End If
    

End Sub
    
Sub HttpClient1_ResponseSuccess (Response As HttpResponse, TaskId As Int)
    Dim resultString As String
    resultString = Response.GetString("UTF8")
    'Work with the result
End Sub

Sub HttpClient1_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    ''Log("Error connecting: " & Reason & " " & StatusCode)
    If Response <> Null Then
        Log(Response.GetString("UTF8"))
        Response.Release
    End If
End Sub
 
Upvote 0

rfresh

Well-Known Member
Licensed User
Longtime User
Erel, when we run our apps on our phones away from the IDE, are we able to view the logs it generates? If so, how?
 
Upvote 0
Top