Location Use cases Best Practices

Location-based apps are absolutely everywhere like transportation apps, geo apps navigation apps, weather apps even dating apps all use location.We will dive into a common-use case that every developer has to address when they are writing location apps.we come up with some best practices. 

1.Use cached location

Use cached locationLet’s start with an obvious one, do you want to know the location of a device? For example, weather app, you want to show the right weather.You need to know, where the phone is?Here, I would say you don’t need location updates, you used cached location.Every time location is obtained from your device, it’s cached somewhere. You can use getLastLocation(). This will give you what you need in a lot of cases.The API has ways of knowing how stale or fresh this is.You save a tonne of battery that way.

2.User-visible(foreground) update

User visible updatesYou have user-visible foreground updates – for example, a mapping app of some kind.So here, because it is foreground, it is okay to use high accuracy, high frequency, and low latency.It’s expensive, but it is okay because, in the foreground, this is pretty much tied to your activities’ life cycle and it will end soon.So typically, what you would do in an activity is you would request location updates, but you would also do the following which is that onStop you remove the updates.Location gathering will keep happening long after your activities there, which is obviously a very, very bad thing to do.

3.Starting updated at a specific location

Starting updates at a specific locationAnother use case is you want to start location update at a specific location. You want to start location update when you’re near home, when you’re near work, near a cricket stadium-whatever. So here, it is a pretty good case of mixing geofencing and location updates.So, typically, what will happen is, imagine you’ve defined a geofence around some area of interest.If a user enters or exits a geofence, location services will let you know, and, at that point, you can say this is a trigger I was waiting for, I’m now going to request location updates.A common pattern for this is the geofence gets triggered, you get notified, you maybe show the user a notification, the user taps on notification, your app opens up to some activity, and, at that point, location updates begin.

4.Activity Recognition location update

Activity RecognitionAnother common use case is, where you want location updates but you only want them tied to a specific user activity – maybe when the user is riding a bike, or driving in a car.Here, we would use the activity-recognition API and combine that with location updates.It would work like the previous example: let’s say you were tracking cycling.Location services would tell you when the user is likely to be on a bicycle, and you can take that and start location updates to the notifications, something comes into the foreground.


5.Awareness API

Awareness APIAndroid has an exposed and an awareness API.It basically senses and infers your context, and it manages system health for you, and it does so in a battery-efficient manner. If you’re dealing with complex scenarios, awareness API may be exactly what you’re looking for.It tracks lots of things: what time of day is it? What is the location of the device? What are are the place nearby? Are there coffee shops or stadiums nearby?Houses of worship?What is the activity of the device, the person on a bike is the person on the car?Are there beacons nearby.It is a person wearing headphones.what is the weather like?You can take all of these contexts and treat of a larger sense of a fence.Basically, you can easily react to changes in multiple aspects of the user’s context and thing generalizes the idea of a fence well beyond conventional geofences which of course are just for the location.

So here is an example so, you create a context fence and it tracks three things you create an activity fence which says tracked that the user is driving it.Create a location fence and says track that this geofence may be a stadium geo being tracked. Then our time geo fence make sure it’s time between this time and this time when all of this thing are true you and your app is let’s say even in the background location services will say all the conditions you specified are true.I’m letting you know you can now do whatever and that whatever could include location update.

6.Snapshot API

Snapshot API that is made possible through awareness and again it’s a simple way to ask for multiple aspects of user’s context from a city again an example you find out what the current places, you find out what the current activity is? if the current place is a shopping mall and the activity is walking hey maybe it’s time to start location updates so that you can tell the user as walks what the stores are nearby or maybe some discounts that you can offer.

The thing is you are using multiple inputs and multiple contexts and that can get pretty expensive for battery because you’re running a lot of different things. If you use awareness API you can minimize battery cost because awareness API is actually pretty battery optimized.

7.Long-running(background) updates tied to specific locations

Long running updatesYou want to find all the Starbucks in the city or you want to find all the ATMs.Android has a solution that involves and a Dynamic geofence says location services make a requirement that you can only 100 geofences at one time there are of course many more ATM many more Starbucks than just a hundred and also maintaining 100 geofences is actually pretty expensive.That’s a lot of scanning that the location services will have to do and that’s going to drain your battery.So the solution is dynamic geofences maybe put a geofence around the city and when the device enters that city dynamically registered geofences in locations inside that city so you have the outer geofences dynamically you put energy offenses if the person leaves the city you can remove those dynamic geofences that are inside because you don’t need them anymore and this is a way you can actually in a very battery efficient way get a lot of geofences get around 100 geofences limit and actually do really pretty amazing things.

8.Long-running background updates.

The problematic one you want long-running background updates with no visible app component so basically think of an app that passively tracks your location for hours or days at a time. So this is a case that gives that keeps people up this the case that is inherently problematic and this is a case where you get into that problem. That I initially refer to that background location gathering is a really really major drain on battery but if you have to do it how do you it so?

Solution: long-running service?

Exposes a method for getting location updates using a PendingIntent and that’s exactly what you should do. So you request location updates you give it a location request you give it a PendingIntent and GMS core location services will wake up your app when an update is found.

In cases like this, what should location request look like what are you gonna do in the background that you gonna do in the background that doesn’t burn battery so you use moderate accuracy low frequency and high latency let’s just look at three of that thing right now.

You do not want accuracy which is priority high accuracy for any background use cases this bad for battery


For frequency, I think a good pattern would be to request updates times an hour let’s say every 15 minutes.Certainly, you should try to get more update through passive location gathering. So that’s why it’s a good idea to set the fastest interval to some small amount this way if others are gathering location you get that location for free doesn’t cost you anything.


Latency this is really really important again imagine that you set your interval to 15-minutes. If you set the max wait time to 1 hour you will get location updates every hour but you’ll updates which are calculated every 15 minutes at a time that’s pretty good for background and that will save battery.

9.Frequent Updates while a user interacts

Frequent updatesWhat if you want frequent updates while a user interacts with other apps?So imagine a Fitness app or Navigation app. So in this kind of a case, you should use a foreground service.This is sort of the recommendation that Android coming up with because android believes that when potentially expensive work is being done on behalf of the user that user should be aware of the work. A foreground service, as you know, requires a persistent notification.The user will be able to see


Related Posts

Creating and Monitoring Geofences

How Location API Works in Android

Understanding Battery Drain when using Location


One Reply to “Location Use cases Best Practices”

  1. hello,
    I’d like make little app similar to “8.Long-running background updates.” but I am new in android so I am unable to write this app from scratch now. Can you attache some example – piece of code?
    Thank you very much

Leave a Reply

Your email address will not be published. Required fields are marked *