Search code examples
androidgpsbroadcastreceiverlocationmanager

Efficient use of LocationManager in Android


I am trying to optimise an Android application which must achieve the following:

  • Every hour, retrieve the GPS location and doSomeWork()

  • At any other time if the device moves more than @MINIMUM_DISTANCE, increase update frequency to something like 5 minutes to log the movement, when movement ends we can return to the hourly updates.

Now, AlarmManager firing every hour works a treat for the first requirement. It's a fine battery efficient method of accomplishing this task. However I'm unsure how to best accomplish the second requirement.

Should I:

  • Register an AlarmManager which fires every 5 minutes in tandem with the hourly timer? Register LocationManager each time and attempt to pull off a location, compare to previous location to determine whether movement has occurred.

  • Register a LocationManager after my hourly wakeup with a minimum distance parameter equal to my required minimum movement.

  • Something else?

I can see pros and cons to each approach.

Using the LocationManager minimum distance parameter seems to me to be the more concise solution but I'm concerned about battery drain, will this keep the GPS running hot the whole time regardless of whether my application is receiving updates? I am also not sure where the best place to register the receiver would be, I understand it's poor practice (not to mention nigh impossible) to keep a service running 100% of the time.

Yes I am new to Android! About to go and test the behaviour of LocationManager.

Thanks in advance.


Solution

  • My solution, which is working well, is to keep NETWORK_PROVIDER (and PASSIVE on the off chance I can get some updates for free) registered and listening any time the GPS_PROVIDER is not. If movement is seen outside the previously retrieved location, I can switch to full GPS to monitor the movement.

    Accuracy is poor, but at least sufficiently good. Battery consumption is remarkably low.