Location-based apps are absolutely everywhere like transportation apps, geo apps navigation apps, weather apps even dating apps all use location.
Location APIs currently allow developers to request location at virtually anytime and make progressive location requests with no barriers.
Background location has major power issues
Background location has been identified as a major contributor to battery drain and power issues.Aggressive use of background location is a major reason why people disable location on their devices.So, in a response to this, the Android team starting with Android O put in place some fairly substantial limits on the gathering of background location.
What about pre O?
The majority are running on Android N or lower.What about those devices? For the foreseeable future, that is going to be the case.This talk is fundamentally about identifying best practices that you could use now in your Android apps when you use location. So that you’re writing your apps in a battery-efficient manner.Let’s dive into this.
Framework vs Fused location
For historical reasons, there are two ways in which you can get location when you’re using Android apps: Framework location and Fuse location.
Framework location is the older one, been there the beginning.It is basically
android.location.locationmanager giving you a wide API surface whereby you as app developers can decide I want to use GPS, I want to use wifi, with I want to use some sensor, and you can get the location as you see fit.This type of location is not optimized for battery, and we discourage you using this.what we would like to you instead is Fused location provider.This is available through GMS core.It is in
Fused location provider provides a narrower surface and sits on top of platform and hardware components.The way this work is you tell Fuse location provider what kind of location we want: forced, find, how frequently you want it.It just figures out what underlying technologies to use and how to do this in the most battery-efficient way.This location provider is highly optimized for battery, and we would like you to use this.
How Fused location Work?
There is a bunk of inputs that go into Fused location.There is Wi-Fi, GPS, Cell, Accelerometer, Gyroscope, Magnetometer.
GPS works great outside. It has some trouble with cities and tall building but in clear skies, it works fantastically, super accurate location but terrible for battery.That is your trade-off-great location accuracy but really bad for the battery.
The coverage for Wi-Fi is mostly indoors.The accuracy is pretty good.You can tell using just Wi-Fi where a person is in a building and what floor they’re on.The power consumption isn’t as bad as GPS but Wi-Fi scans are fairly expensive.It is not free: it does cost something.
This is available indoors and outdoors, available almost everywhere.The accuracy with the cell is not so great.You’re not going to get a location which is accurate to within a few feet, you will get the location to a neighborhood level or a city block, etcetera. But it is great for power consumption.It basically uses very, very little power, So it is fantastic for that.
Then you have the sensor which plays an extremely important role in making Fused location provider do the right thing and do the right thing for battery. You have an Accelerometer which measures changes in velocity and position. You have Gyroscope which measures changes in orientation in the device, and the Magnetometer which allows you to use the device as a compass.By and large, most of these sensors have very, very little battery cost.Fused location provider will use these sensors in conjunction with Wi-Fi and GPS to use the best as it can with minimal battery usage.
If you requested Fine location, accurate to within a few meters, Fuse location would use GPS and Wi-FI but GPS and Wi-Fi work better when you combine them with sensors.So, for instance, I mentioned GPS is a little bit jumpy when you’re in an environment with tall buildings.Imagine Hong Kong, Mumbai, New York City.Those are challenging environments for GPS. When GPS gets a little flaky, Fused location, instead of making expensive GPS scans, will say,”Let me see what the sensor data tell me.What is the accelerometer telling me?” It pieces together a pretty good sense of what is that is happening. The same with Wi-Fi, Wi-Fi can be a bit jumpy.When it gets jumpy, Fused location provider will not do excessive Wi-Fi scans but instead look at the sensor data and look at the what the device might be doing.Indoor maps sort of work like that.There was a time when Google maps would give you – if you want to a shopping mall, It would say you’re in this mall. Now it says you’re right here in this shopping mall on the third floor.It will do things like that.A lot of that is driven by sensors.If the location had to be pulled constantly, if the Wi-Fi scans had to be constantly done, that would be terrible for battery.It doesn’t have to do that.Once it gets a Wi-Fi fix, it can look to the sensor data and look in a battery-efficient way look where you are? you turning or moving, etcetera? That’s basically what it is. The Summary of this is, where possible, give the choice between framework location and Fused location, you should always use Fused location.
There’s one higher-level API that is the geofencing API, and that should be an important tool for anyone building location apps.It is a case where you can define a circular region somewhere and say whenever the device enters or leaves in the region, or sits in this region for a certain number of hours, do something.let me know and that basically is how geofencing works.Geofencing is built on top of Fused location and it’s highly optimized for battery. So the basically the way it works is the API monitors device proximity to a geofence.The closer you are to the geofence, the more expensive it is.It basically figures out what is your speed? Are you in the car? Are you walking? How far are you from the geofence. It optimizes for battery in terms of among Torrington the geofence in the background.