📜 ⬆️ ⬇️

Increased battery life in Android M



According to many developers, in most cases, poor application optimization leads to a too fast discharge of smartphone batteries. In May of this year, Google hosted the regular annual Google I / O conference, at which many interesting things were announced for developers. In particular, when it came to Android M, the performance of mobile devices and battery life was one of the central themes. Let's take a look at the new features and tools that will help users and developers to squeeze even more out of mobile device batteries.

Doze


This is one of the main tools in the new OS to curb application activity when the device is in sleep mode. On smartphones running Lollipop and older versions of Android, any application can take the device out of sleep for the sake of its update, which allows you to keep the data current. Doze analyzes the current state of the device and data from the accelerometer, determines when the smartphone is not in use, and sends it to "deep sleep". In this mode, updating applications is prohibited until the device is awakened by some other, higher priority event. In the "deep sleep" mode, network activity, sleep blocking, warnings and JobScheduler tasks are disabled.

As the experience suggests, frequently updated in the background applications are one of the main reasons for the rapid discharge of the battery. And Google holds the same opinion. At the conference, information was given that the Nexus 9 tablets running Doze worked almost twice as long in standby mode.
')
To test this feature, upgrade to Android M, and then use the ADB command to disconnect the charging device:

adb shell dumpsys battery unplug 

Doze can translate a device in one of several modes:


You can manually switch between these states:

 adb shell dumpsys deviceidle step 

How long does it take to transfer the device to this mode?

 adb shell dumpsys deviceidle 

This allows you to get a lot of interesting information about Doze. For example, on Nexus 6 (with the display turned on) the following results were obtained:

 Whitelist system apps: com.android.providers.downloads com.android.vending com.google.android.gms Whitelist user apps: com.facebook.katana Whitelist app uids: UID=10012: true UID=10016: true UID=10026: true UID=10100: true mSigMotionSensor={Sensor name="Invensense Significant Motion Detector", vendor="Invensense Inc.", version=1, type=17, maxRange=1.0, resolution=1.0, power=0.3, minDelay=-1} mCurDisplay=Display id 0: DisplayInfo{"Built-in Screen", uniqueId "local:0", app 1440 x 2392, real 1440 x 2560, largest app 2413 x 2308, smallest app 1440 x 1356, mode 1, defaultMode 1, modes [{id=1, width=1440, height=2560, fps=60.0}], rotation 0, density 560 (494.27 x 492.606) dpi, layerStack 0, appVsyncOff 7500000, presDeadline 12666667, type BUILT_IN, state ON, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}, DisplayMetrics{density=3.5, width=1440, height=2392, scaledDensity=3.5, xdpi=494.27, ydpi=492.606}, isValid=true mIdleDisabled=false mScreenOn=true mCharging=false mSigMotionActive=false mState=ACTIVE mInactiveTimeout=+30m0s0ms 

The first part lists the processes accredited by Doze. Their activity is not limited in any way. Please note that Facebook has been manually added to this list (Settings -> Battery -> Facebook -> activate “Disable optimization”). Next comes the section with information from the accelerometer and the display status. These data will be used for the subsequent withdrawal of the device from the state of "deep sleep". In this example, the display is on, which means Doze is in ACTIVE mode.

The device inactivity period is set to 30 minutes. If you turn off the display, this section changes:

 mState=INACTIVE mInactiveTimeout=+30m0s0ms mNextAlarmTime=+28m26s193ms 

With the display off, the device goes into INACTIVE mode and can switch to the “deep sleep” mode. The timeout is also equal to 30 minutes, and, according to the timer, about 1.5 minutes elapsed from this period. Manually translate the device into IDLE_PENDING:

 mState=IDLE_PENDING mInactiveTimeout=+30m0s0ms mNextAlarmTime=+24m34s189ms mNextIdlePendingDelay=+5m0s0ms mNextIdleDelay=+60m0s0ms 

After 30 minutes of being in this mode, the device will go to IDLE. Thus, a total of 60 minutes is required to go into a “deep sleep”. Transition:

 mState=IDLE mInactiveTimeout=+30m0s0ms mNextAlarmTime=+59m42s953ms mNextIdlePendingDelay=+5m0s0ms mNextIdleDelay=+2h0m0s0ms 

After 60 minutes, the trigger will fire, the device will wake up and send all the accumulated messages, initializes the events and triggers. The mNextIdleDelay parameter indicates that the next awakening from “deep sleep” will occur in 2 hours. It turns out the following cyclical mode changes: 1, 2, 4 and 6 hours. That is, the largest interval between awakenings is 6 hours, 4 times a day.

How do applications behave when the device exits deep sleep mode?

You can independently test your application by sending the device into a “deep sleep”:

 adb shell am set-idle <packageName> true 

Using the same command, you can wake it up (by entering false instead of true ), and see how different processes are activated, requests are sent and executed.

As you can see, Doze is a great tool for saving battery when the device is turned off.

Standby for applications


In current versions of Android, any application has access to the radio module, even in the background. In other words, any application you have long since downloaded and forgotten can quietly transmit some data several times a day without your knowledge. With Doze, you can block the launch of applications, but they can still run processes and update when the user turns on the device display. With the help of the new standby function for applications (App standby), it is possible for applications that have not started in active mode for a certain period of time (in days) to force a standby mode. In this mode, applications are limited to accessing the Internet or launching any processes until the smartphone is connected to the charger. This saves device battery.

How does the standby mode for applications work? If you run:

 adb shell dumpsys usagestats 

You can get a lot of information about application activity for the last day / week / month and year.

for example
 user=0 In-memory daily stats timeRange="6/1/2015, 5:00 - 11:16 PM" packages package=com.google.android.googlequicksearchbox totalTime="00:36" lastTime="6/1/2015, 10:56 PM" inactiveTime="01:05" package=com.android.providers.calendar totalTime="00:00" lastTime="6/1/2015, 10:20 PM" inactiveTime="02:26" package=com.android.providers.media totalTime="00:00" lastTime="6/1/2015, 10:20 PM" inactiveTime="02:20" package=com.android.providers.downloads totalTime="00:00" lastTime="6/1/2015, 10:20 PM" inactiveTime="02:20" package=com.android.defcontainer totalTime="00:00" lastTime="6/1/2015, 11:10 PM" inactiveTime="00:00" package=android totalTime="00:00" lastTime="6/1/2015, 10:32 PM" inactiveTime="01:44" package=com.urbandroid.inline totalTime="00:00" lastTime="6/1/2015, 11:14 PM" inactiveTime="00:00" package=com.google.android.gm totalTime="00:00" lastTime="6/1/2015, 9:01 PM" inactiveTime="06:36" <snip> In-memory weekly stats timeRange="5/29/2015, 2:33 PM - 6/1/2015, 11:16 PM" packages package=com.amazon.mShop.android.shopping totalTime="01:04" lastTime="5/29/2015, 3:24 PM" inactiveTime="3:55:51" package=com.google.android.youtube totalTime="55:51" lastTime="5/31/2015, 2:04 AM" inactiveTime="1:09:00" package=com.android.providers.telephony totalTime="00:00" lastTime="5/29/2015, 2:56 PM" inactiveTime="5:37:10" package=com.android.sdm.plugins.connmo totalTime="00:00" lastTime="5/29/2015, 3:13 PM" inactiveTime="5:46:30" package=com.google.android.googlequicksearchbox totalTime="33:12" lastTime="6/1/2015, 10:56 PM" inactiveTime="01:05" package=com.android.providers.calendar totalTime="00:00" lastTime="6/1/2015, 10:20 PM" inactiveTime="02:26" package=com.android.providers.media totalTime="00:00" lastTime="6/1/2015, 10:20 PM" inactiveTime="02:20" package=com.google.android.apps.docs.editors.docs totalTime="00:00" lastTime="5/29/2015, 3:13 PM" inactiveTime="5:46:30" package=com.google.android.onetimeinitializer totalTime="00:00" lastTime="5/29/2015, 3:13 PM" inactiveTime="5:46:30" 

The algorithm built into the OS determines the date of the last launch of the application and, if necessary, puts it into inactive mode. The list of such applications can be viewed by entering the settings menu in the “Inactive apps” item.



Application performance


Probably, all of you find yourself in a situation where the smartphone battery sits down too quickly, but it is not clear why this happens. If you go to Settings -> Battery, you can get detailed energy consumption statistics for each application:



In this case, the main consumers of the battery: the CPU (including in the background), the network protocol and the radio module. Presented statistics collected for 28 hours, since the battery is fully charged.
Also in Android M, a menu is now available, with which you can “ignore optimization”. In this case, Doze and standby for applications are disabled. This allows developers to test the performance of their applications both with the optimization turned on and off, finding out how this affects the battery life. And at the same time how quickly the data in the application become obsolete.

GCM Network Manager


Wonderful novelty, for the use of which does not even require the presence of Android M - it can be run on any version of the OS, up to 2.3!

In the Lollipop released late last year, the JobScheduler API appeared. On Android 5.0 and higher, this API separates all hardware warnings and sleep blocking from applications, transferring them to the OS level. This allows the operating system to aggregate all these events from different applications, thereby reducing the number of awakenings and warnings, and thus saving battery power. Remarkably, it only works on Lollipop and above (at the time of this writing, this is about 12.4% of Android devices).

To provide the same level of performance for network connections, GCM Network Manager will be added to Google Play Services version 7.5. It uses a framework similar to JobScheduler, but it will work on any devices where Google Play Services is installed (up to Android 2.3, that is, 99% of all Android devices). GCM Network Manager supports the same scenarios and modes as JobScheduler, and also allows you to limit the number of network connections until the smartphone is connected to the charger, and also to use Wi-Fi exclusively for large updates.

* * *

All these innovations suggest that Google is concerned with the performance of applications and is trying to give developers the right tools. We can also better control the behavior of applications for the sake of reducing the power consumption of smartphones.

Source: https://habr.com/ru/post/264023/


All Articles