Battery Life optimizations in Android -

Gaurav Bansal
May 16, 2018 · 5 min read

First Android Smartphone HTC Dream was launched in 2008, back then It had 256 MB RAM and 528 MHz CPU.Since then android smartphone RAM has been upgraded upto 6GB RAM and 2.8GHz CPU. Now users don’t bother too much about app size because of more than sufficient RAM and internal storage is available but still user is concerned too much about power consumption.

Battery Android

Before discussing about all the changes made in Android P, we will discuss about changes that are already made in previous android versions.

What is already done- A lot of efforts are already made on the Android OS end by Google to improve battery performance of device.

  • Job Scheduler- In lollipop job scheduler was introduced to optimize background task.using Job scheduler we can schedule a particular task to be performed on certain time with certain conditions like whenever device is on charge mode or wifi mode etc.
Battery optimization DONE
  • Doze Mode and App standby- Doze mode is a new power saving mechanism introduced in Android 6.0. In this mode, background behaviors of all apps (except white-listed) are strictly prohibited, until next user interaction or charging. The device will also periodically (in hours) exit doze mode briefly.
    Doze was introduced in Android Marshmallow .Doze reduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time(Doze mode).

App Standby- App standby defers background network activity for apps with which the user has not recently interacted.

  1. High priority GCM messages (required for instant messaging apps) will be delivered even device is in Doze mode.Normal GCM will be batched to maintenance windows.
  2. Foreground services such as music is exempted by Doze and app standby mode.
  3. To start a alarm in Doze mode separate Alarm API is provided setAndAllowWhileIdle() or setExactAndAllowWhileIdle().

Doze on the go-Android system uses a low power “significant motion sensor” (if present in hardware) to track movement and exits Doze Mode if significant movement is detected, because the stock Doze Mode is designed for battery-saving in stationary state. Doze on the Go effectively removes this restriction and keeps your device in Doze Mode even during movement.Android 7.0 introduced native support of Doze on the Go and a new light doze mode, which blocks network access but not wake-up timers of the app. When movement is detected, it exits deep doze and keeps in light doze, and then goes back into deep doze after device is stationary for some time.

Background Execution Limits- As one of the changes that Android 8.0 (API level 26) introduces to improve battery life, when your app enters the cached state, with no active components, the system releases any wake locks that the app holds.

In addition, to improve device performance, the system limits certain behaviors by apps that are not running in the foreground. Specifically:

  • Apps that are running in the background now have limits on how freely they can access background services.
  • Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).

Note:-By default, these restrictions only apply to apps that target O. However, users can enable these restrictions for any app from the Settings screen, even if the app has not targetted O.

Background location limits-In order to preserve battery, user experience, and system health, background apps receive location updates less frequently when used on a device running Android 8.0. This behavior change affects all apps that receive location updates, including Google Play services.

In Android P-

  • Adaptive Battery- It intelligently aligns power consumption with app usage.App can still run in the background when they need to and users don’t need to micro-manage.

Adaptive battery usage the concept of App standby buckets(introduced with Doze mode).Every app on the device will be assigned to one of four buckets(Active,Working set,Frequent,Rare).Each bucket has different limits on background activity.Apps in the rare buckets will have most restrictions.

Adaptive battery

Android OS will use ML model to assign apps to these buckets based on their predicted usage. These all restrictions are lifted when device is power plugged.

How bucketing done using Machine learning- ML Model used is a two layer deep convolutional neural net with a feed forward neural net on top and this is used to predict the probability that an app will be opened in a given interval.This model is build using Tensorflow and will work on user device.Model is trained on millions of sequences of app open and transitions to discover the patterns of how user behave.
This ML model is purely unbiased there is no favouritism of one app over another.All personally identifiable information is removed before the model is trained.

  • Battery saver- To extends your battery life,battery saver turns off some device features and restricts apps.Location is off when device screen is off.Ambient display is also turned off when battery saver mode is on.
    Developer can explicitly switch app to dark theme when device is in battery saver mode because dark theme consume less power.
Battery saver mode API
  • Additional Background restrictions-
    -
    User should have control over apps.
    - Legacy app doesn’t get a pass.
    - Apps don’t get restricted in first place

Background restrictions criteria-
1.Apps targeting pre -Android oreo and using background services.
2.Excessive wakelocks(>1 hr in background).

When Background restrictions enabled all the following things are restricted-

  • Background jobs,alarms,services and network access
  • Most location related updates(explicit intents)
  • Foreground services.

For battery optimization on apps end a new Work manager APIs also introduced for background tasks in Android P.

So that’s all about what Android had done on OS end so your device will drain less battery.

Read more posts about what all new things and changes in android from Google I/O 2018.

Happy coding!!!

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Gaurav Bansal

Written by

SDE-III at Olx.in

MindOrks

MindOrks

Our community publishes stories worth reading on software development and design. Android | Machine Learning | #MakeEveryoneCode

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade