views:

215

answers:

2

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.

+1  A: 

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.

radek-k
Thanks @radek-k, I had seen that documentation but from the description I assumed that setting a high minTime would indeed conserve battery. Your description makes sense though, but maybe I'll loop through the cycle every 15 minutes instead just to be more conservative on the battery.
Toby J
In any of your approaches the GPS radio is on, isn't it? So it might be consuming power anyway. If the delay between enabling and retreiving a location (your 2nd approach) is too low, it could be better to keep it on (how many time does it take to obtain a GPS fix?)
Guido
A: 

I was wondering if you ever got this to work..?? If so...can you post the code..??

Thanks, Brian

Biggs
Sorry, I played around with this a bit more but then couldn't get it working in time to be useful for me. I used what appeared to be verbatim code samples from other sites but they never went off when they should.
Toby J