4

I'm creating an application (For educational purposes) which records the user's location every 30 minutes, and enables the user the view all the locations on the map. I don't want updates to be more frequent than 30 minutes, but they are.

This is how I call requestLocationUpdates:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,  30 * 60 * 1000,  0, pe);

Now, the documentation clearly says:

The elapsed time between location updates will never be less than minTime

But I did see some answers here on SO stating differently (This answer, for example).

It seems like I'm getting updates whenever they are available from the GPS. The GPS icon never turns off and the updates rate becomes greater than 1 update/second. So I have 2 questions here:

  1. The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?
  2. Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?
Community
  • 1
  • 1
Jong
  • 9,045
  • 3
  • 34
  • 66

2 Answers2

5

The minTime parameter doesn't fulfill its purpose, not even as a hint (Yea, a hint to 30 minutes update rate leads to more than update a second...). What does it do, then?

From Jellybean onwards devices must observe the minTime parameter, so it does have a purpose (now).

Is there any other way to do it? I don't want the GPS to be enabled all the time because it will consume my battery too fast. Maybe I could schedule alarms repeating each 30 minutes, and call requestSingleUpdate?

Yes, use a Handler to request one update with requestSingleUpdate() every 30 minutes.

I've tackled this before in a previous question, let me know if the code in that answer helps and if you have any questions it doesn't address.

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks, I'll look there. I don't think a Handler is an option because my application may be dead between updates, but using the `AlarmManager` is similar. My device runs 2.3.3, But interesting, I would never guess your answer to the first question. – Jong Nov 25 '12 at 21:17
  • In that case, an AlarmManager is the way to go. You can also use a background Service to fetch and save the new location if you don't want to bother the user. – Sam Nov 25 '12 at 21:27
1

The second parameter, min distance difference in meters is set to zero, it causes constant updates. Prefer using requestSingleUpdate within a timer+handler on a desired period in minutes.