Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain Android GPS location once every few minutes

I would like to write an app on Android to upload my GPS location to an external website once every ~5 minutes. This needs to have as minimal an impact on battery life as possible, but it also needs to work without any user interaction. (Background: I'm competing in an Ironman triathlon which will take me about 14 hours to complete, and want to broadcast my location in near-real-time but without having to worry about fiddling with my phone.)

So my initial thought is to write a Service which uses LocationManager.requestLocationUpdates() with a minTime of 5 minutes, but will this actually wake the device up every 5 minutes for my service to do its job?

It sounds like I would also need to use AlarmManager.setInexactRepeating() to make sure my service is awake while it completes its task but how does that play with requestLocationUpdates()? Should I instead set minTime=0 on requestLocationUpdates() but then go back to sleep as soon as the next update is obtained?

Any general guidance on how to design this is greatly appreciated. I'm a competent Java programmer & will be using Google Maps on the server to plot my location, but am pretty new to Android development so I'm basically looking for a high-level plan on how to architect the client app.

like image 384
Tobias J Avatar asked Aug 23 '10 13:08

Tobias J


People also ask

How do I turn on GPS automatically on Android?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do I refresh my GPS location?

Refresh your GPS DataIn the app, tap anywhere on the screen, then tap the menu icon and hit Manage A-GPS state. Tap Reset, then when that's done, return to the Manage A-GPS state menu and tap Download. Your GPS data should now be refreshed. If it starts acting up again, just repeat this process.


1 Answers

Your service must be alive all the time you want to receive updates.

http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates%28java.lang.String,%20long,%20float,%20android.location.LocationListener%29

You can tell how often you want to be informed of location change with minTime parameter. It does not however decrease battery consumption. GPS is enabled unless you use removeUpdates method no matter how often you want to receive updates.

You can use another approache:enable GPS using method above, read one value, use removeUpdates method, wait 5 minutes and all over again. Delay between enabling and retreiving a location can be between few seconds to few minutes.

like image 131
plugmind Avatar answered Oct 07 '22 20:10

plugmind