0

I would implement in my app GPS, I would know user position every 1 minute and I am not sure how could I do it preserving battery. Should I destroy "requestLocationUpdates" every time I get location and after a minute call it again ? Are there better way?

MadCode
  • 7
  • 1
  • 6

2 Answers2

0

First Solution

If you check the documentation of LocationManager (Link below) you would see that it takes an argument minTime which indicates the minimum time interval between location updates, in milliseconds.

If you want an update every 1 minute, then set this argument to 60*1000.

However prior to JellyBean, minTime is not effective.

Link: http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String, long, float, android.location.LocationListener)

Second Solution

A second solution which I recommend is to use a Handler and a Looper. Basically every certain amount of time X you request a Location update then you cancel the location update until you request it again after X time, and it loops again. Check the answer of this question Android Location Listener call very often

Community
  • 1
  • 1
Ziad Halabi
  • 964
  • 11
  • 31
  • Thank you but it's the same setting this value or just calling requestLocationUpdates every minute?. is the battery influenced in the same way? – MadCode Mar 29 '15 at 12:00
  • After JellyBean, the minTime is effective. However, a second solution which I recommend is to use a Handler and a Looper. Basically every certain amount of time X you request a Location update then you cancel the location update until you request it again after X time, and it loops again. Check the answer of this question (http://stackoverflow.com/questions/13093613/android-location-listener-call-very-often) – Ziad Halabi Mar 29 '15 at 12:12
0

If you know the user location every minute then:- Yes you should destroy "requestLocationUpdates" and implement an AlarmManager which will requestUpdate for Location

Pankaj
  • 7,908
  • 6
  • 42
  • 65