Battery saving feature in Android

Tram Ho

What is doze mode?

Doze mode is an energy saving feature introduced on Android 6.0 (API level 23).

The main idea of ​​this feature is to reduce and save energy when users do not need or are not using the phone. Assuming that when the user stored the phone and went somewhere, about 1 hour later, the user arrived and turned on the phone, the battery should not be reduced too much.

How does mode mode work

There are 2 stages of Doze mode The main stage (aka Deep Doze) introduced on Android 6.0 will activate when the following 3 conditions are met.

  • Device is not connected to charger
  • The screen is locked
  • No interactions were noted for a period of time (approximately 30 minutes).

When in Doze mode, the system will delay battery-consuming activities such as network calls, wake-locks, alarms (AlarmManager) to save energy. The system will periodically give Doze mode a short period of time (called maintain window) to perform these delayed operations. Over time, the system will schedule these maintain windows less and less.

The system will exit Doze mode when users start using the device, such as moving, unlocking the screen or connecting to a charger.

On Android 7 (Nougat) added a lighter version of Doze mode called Light Doze. Light Doze is activated when the screen is off and the device is not connected to a charging source. Light Doze is similar to Deep Doze, but it will have fewer controls to be able to start and maintain the window more often.

Doze Mode switches from Light Doze to Deep Doze once the conditions of the Deep Doze are met.

The operation is restricted in Doze mode

  • The network call
  • Wake lock is disabled
  • Alarms in AlarmManager will be restricted when in the maintain window (except for the functions setAndAllowWhileInIdle (), setExactAndAllowWhileIdle (), setAlarmClock ())
  • The system will not automatically scan for wifi or GPS
  • JobScheduler’s blocking system

How to perform important actions while in Doze mode

As above for AlarmManager, we can still call the function setAndAllowWhileIdle () or setExactAndAllowWhileIdle () to perform actions in Doze mode.

However, we cannot make a network call through a while-idle alarm when in Doze mode, it will lead to UnknownHostException. If you still want it, you should use JobScheduler or WorkManager

For actions that need to be enabled via the backend, you should use FCM. By setting high-priority messages on FCM, you can send or process messages immediately even in Doze mode.

For apps that need to be constantly active (like the map navigation app), you can use the Foreground Service. The Foreground Service is exempt from restrictions in Doze mode.

For other cases where the above k does not meet, you can request the user of that app whitelist to be able to make network calls or wakelocks.

You can request permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS so that users can turn off Doze mode for the app.

What is App Standby?

App Standby is another power-saving function introduced in Android 6.0. The idea is that the system will keep a list of idling apps that users rarely use or rarely interact with. processing battery-consuming tasks of these apps (network, background job …)

An idling app is an app that the user does not interact with and there is no pending notification in the lock screen

App idling has only one maintain window in a day to run all jobs or network calls are delayed. This way the system minimizes many energy-consuming tasks.

An app exits idle mode when the device is connected to a power source or when the user uses the app or show notification.

What is the App Standby Buckets?

This is also another new saving function introduced in Android 9. It’s like the App Standby 2.0 function. Its idea is thanks to the system that determines the usage of recent apps. The system will classify the apps into buckets with the following categories

  • Active: the app is currently used or recently used
  • Working Set: the app is used daily
  • Frequent: The app is used frequently but not daily
  • Rare: rarely used app
  • Never: app is installed but not yet used

The limited operation in the App Standby Buckets

  • Active: there are no restrictions on network, background job, timer for these apps
  • Working Set: There are slight restrictions based on the ability to run jobs (up to 2 hours) and alarms (limited up to 6 minutes)
  • Frequent: There are bigger limitations like the ability to run jobs (up to 8 hours), alarms (up to 30 minutes) and high priority message FCM will only receive 10 messages a day.
  • Rare: Ability to run jobs (up to 24 hours), alarms (up to 2 hours), receive high priority message 5 messages a day, network access (up to 24 hours)

Apps that are in the Doze mode whitelist are not affected by these restrictions

If you want to know which category the app is in, you can use the “Standby Apps” setting in Developer Options.

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo