Android Question GPS Location from a Service

Bernard Harris

Member
Licensed User
Longtime User
Should I be able to do the following with the GPS library? I'm in an office at the moment so I can't test the actual movement of the device but I'm hoping the below will work. Let me know if my logic is incorrect.

This is for an automated service that retrieves the GPS location every 30 minutes. I took most of this code from the GPS tutorial.

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

Sub Service_Start
GPS1.Start(0,0)
Sleep(10000) ' wait 10 seconds for location to change.

' Do my processing using the Latitude and Longitude variables retrieved from the GPS_LocationChanged sub.

GPS1.Stop

StartServiceAt(blah, blah, blah)
End Sub

Sub GPS_LocationChanged (Location1 As Location)
Latitude = Location1.Latitude
Longitude = Location1.Longitude
End Sub
 

Bernard Harris

Member
Licensed User
Longtime User
I was able to walk outside (it's only 4 degrees F here) and I did get a valid location. I am getting too many transactions at once so the service seem to trigger more than it should. It might be related to the fact I have the StartService in the Main.Activity_Resume so will try moving that to the Activity_Create and let the StartServiceAt take care of the frequency.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
GPS1.Start(0,0)
That setting is the highest frequency for a location change (0 seconds, 0 distance moved) - and a BIG consumption of battery.

Sleep isn't required since the location_changed event will fire whenever there is new data. I see you stop GPS, but that "should" be in the change event - after you get some data. Call any sub from location changed to process your new data - then shut down GPS - since you don't know how long it will take for the GPS to give you a new location.

You could also use FusedLocationProvider but it is more complicated to implement - yet doesn't use high resources like the GPS object - if you wanted to leave it on full time (not shut down). However in your case, using GPS object every thirty minutes shouldn't harm anything.

From one Harris to another...
 
Upvote 0

Bernard Harris

Member
Licensed User
Longtime User
Thanks for the response Harris. :)

The reason I sleep there is to make sure the process has enough time before the next step saves the new location. I wanted to make sure that there is an opportunity to get the current location. If this isn't needed I can definitely take it out.

Are you saying I should do a GPS1.Stop in the LocationChanged event? Oh I see, do the actual update of the data within the LocationChanged event (in a sub) and then stop it here, is that correct?

I thought about the FusedLocationProvider but I agree that it looks more complicated. So far in my testing I am getting a location response every 30 minutes and it's not drawing down the battery much at all. Since it's a device that will be readily available for charging I'm not too concerned with it, as long as it's not too much of a drain.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
then stop it here, is that correct?
Yes, correct.

Like stated: The GPS system may take some time to start. If it is the first time started - it may need to download the azimuths table, aquire enough sats to get a fix and THEN fire a locationchanged event. Only the event will tell you when this has happened - immediately - or 5 minutes from start - or never if the device is inside a metal building.... That's what this event is for - to notify you upon new data...
 
Upvote 0

Bernard Harris

Member
Licensed User
Longtime User
Okay, now I understand. So basically leave the GPS started until it actually is changed, then record the change, stop it, and then set my service to restart in 30 minutes.

This way it will start at 30 minutes and when it changes stop and reset the timer.

Another question then - you mentioned the Start(0,0) using the biggest amount of battery. Is there a more appropriate setting for this, that will help to reserve battery and still receive a good notification that change happened? Or should I just play with it? I'm not sure what the distance would represent - feet, meters, or miles.
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Don't worry about it. It is more of an issue when GPS is running full time AND NOT connected to a charger...
 
Upvote 0
Top