When you think of building or developing location based Android app, does it make you think about how to do efficient location tracking in the app which does not hamper battery of device no matter who you are, app developer or Entrepreneur who is building a concept?

Android battery optimization is a major problem in building location based apps. However this could be resolved and battery optimization can be done at some extend extent by implementing some smart logic depending on the requirement of location tracking in the application.

Location tracking in your app can be implemented in following ways depending on the app’s requirement.
1. Request one shot location of device wherever needed
2. Timer/service based approach for continuous location tracking
3. Geo fence based approach for continuous location tracking

Request one shot location of device wherever needed –

If app demands requesting device’s current location on screen say some API needs lat-long as input or need to use lat-long natively to display some info about current location of screen, map screen etc., app can request location manager to get current location of device in the same activity class. As long as location is used on that particular screen and app does not demand continuous location tracking in background or foreground, this approach could best suit to the requirement. App could just decide some param which is needed to be set to request location using location manager class like accuracy, interval rate at which location client tries to obtain location updates, fastest time interval, priority of location request etc.

Advantages –

1. Not using continuous tracking saves battery used by app.
2. Best suited when location is needed on a particular or few screens

Disadvantages –

1. May give less accurate location as it takes one shot location wherever needed. Though accuracy can be increased by keep requesting location updates till it reaches desired accuracy.

Timer/service based approach for continuous location tracking –

If app demands continuous location tracking in foreground as well as background, you can write your own service to track location on timely interval. In such a implementation, service starts listening to location updates for sometime interval and turns off all the updates for sometime. Turning off updates here means removing all callbacks to location manager. After the timer, when location updates are off, has elapsed, it will again start requesting location updates. Time interval for both the timers can be decided depending on desired accuracy of location app demands, also how frequently app is using current location etc.

Advantages –

1. Use of own service for location tracking gives ability to track location in background as well.
2. Desired accuracy can be met if timer interval is decided accordingly.
3. Location is the most recent and is updated when it is actually being used any time anywhere in app.
4. Battery optimization can be achieved at some extent if timer intervals are decided wisely.

Disadvantages –

1. App needs keep running location tracking service all the time.
2. Less efficient compared to geo fence based approach for continuous location tracking.
3. If service stops due to some reason and no code is written to start service if its stopped, app won't receive location updates.

Geo fence based approach for continuous location tracking –

Google Play Services has made it easy to monitor geo location area of certain radius which is called Geo fence. Same geo fence concept can not only be used for monitoring enter-exit events of created geo fence but also it can be used for efficient location tracking. When app demands continuous location tracking both in background and foreground instead of writing own service to monitor location updates, you can create small geo fence of some radius say 250 meters, again this radius depends on your app’s requirement. If your app needs to track user’s location for very small area this geofence radius can be small otherwise greater radius will also work. When first time user gets device’s location (this location could be of desired accuracy), it will create geofence at that point with the pre-decided radius and turn off location updates. Exit event of such geofence is breached which means if user exits the geofence, the application will destroy the same geofence and will start location updates. The Application will create a new geo fence for the new geopoint. This way app does not need to keep tracking device’s location all the time using a background service. Instead once geofence is created Google Play service will take care of location tracking until and unless the user does not exit geofence.

Advantages –

1. No need of writing own service for continuous location tracking.
2. Better battery optimization as compared to timer/service based approach.

Disadvantages –

1. This approach is not suitable when the app demands location tracking for minute location updates. For example, when the app tracks food places and specially when the area is highly dense with food places.

In this way, depending on your app’s requirement above mentioned techniques can help you to develop efficient location tracking in your app.

zp8497586rq