DominicM
DominicM

Reputation: 2196

Android: Get GPS Location from Service in a Fixed Intervall

I want to receive gps data like every 3 minutes from a service over a long time (3-4 hours). It all works fine but I have some questions to which method is the best regarding battery lifetime.

  1. Start the service, inside onCreate of the service create a partial wakelock, start a timer that is executed every 3 minutes. Get position data from inside the timer. When the service is stopped (after 3-4 hours) release the wake lock.

  2. Use AlarmManager to start the service every 3 minutes. Inside the service create a partial wakelock, get the position data and then release the wake lock and stop the service.

  3. Same as #1 but instead of a timer set the intervall through the requestLocationUpdates() method. (The reason why I don't like this method is that when I use a timer I can get like 5 positions (every 3 minutes) and pick the most accurate but when I set the intervall with requestLocationUpdates() I only get one position, which might be inaccurate, or am I wrong?)

I don't like the idea of leaving a partial wakelock for 3-4 hours thats why I think that #2 might be a better solution.

Upvotes: 1

Views: 875

Answers (1)

Jenny Casarino
Jenny Casarino

Reputation: 115

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000; // 1 sec
LocationManager locationManager; 
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                         MIN_TIME_BW_UPDATES,
                                         MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

Upvotes: 5

Related Questions