How your phone sleeps with you!

Malith Thilakarathne
5 min readSep 13, 2017

--

image credits:- FitHog

Android is trying to leave you live your life…

Before Android Marshmallow, if you left your phone alone and go play cricket at 30% battery… What you do? play cricket. What your phone does? no idea that you aren't around so it keeps delivering FB, Twitter, WhatsApp, Viber notifications and keep syncing Apps … When you finish playing cricket, You are super duper tired! And your phone also, now it has only 15% battery! whaaaat ?
Same happens when you sleep at night right? you want to sleep but, your phone keeps throwing notifications at you if you've kept data/WiFi on.

Multiple Apps does its own background network access any time.

Every time your device activates the mobile radio to do any networking activity, even for a very small amount of data packet, there’s a fixed overhead cost every time you wake up the device.
So, when each and every App does this, it’s obvious that your battery will get worn out soon.

App 1 and App 3 delayed their background task and do it later with App 2. So network overhead cost reduced from 3 to 1.

But, the more applications can batch together any of their background activity and coalesce them, the more efficient the system becomes and battery can be really, really saved.

The Volta team inside Android have introduced “DOZE” with Android Marshmallow, applying this phenomena.

“Ideal” Doze mode — zero usage of battery when user is not even remotely using the device.

If you are Android 6+ user, you must have noticed that when you pick up your phone after leaving it for some time alone, it suddenly floods you with notifications like it has identified you…

So, you feel that you are in control. Not that you run to our phone for each and every single notification!

image:- talkandroid.com

What is Doze?

Doze extends battery life by deferring application background CPU usage and network activity when a device is unused for long periods.

If Doze mode is going to continue for a long time, those delayed jobs are batched up and executed periodically (in the maintenance window). And it continues the sleep/maintenance sequence, increasing the length of sleep each time

But, as soon as you pick the phone again, android exists the doze mode and you’ll receive any batched up delayed notifications. If you are a user of Android 6 and above, you've already experienced this!

Recurring maintenance window occurring less and less frequently (image credits:- developers.android)

Doze is triggered when,

  1. Device is not charging
  2. Screen is off
  3. Device is not moved in a while

When a device is in doze mode:

  1. Network access is suspended
  2. Wakelocks are ignored
  3. Alarms are delayed
  4. Syncs and Jobs are delayed

These are achieved through delaying,

  1. SyncAdapter
  2. JobScheduler
  3. AlarmManager

Doze exists when,

  1. The device is plugged in or
  2. Picked up or
  3. Device screen turns on or
  4. Alarm clock set to go off soon ( exits doze well before that because every waking App get a chance to sync before that user wakes up)

System services (phone, SMS…) are exempted from Doze by default. You can also exempt specific applications from Doze in the Settings menu.

Significant battery saving in doze mode

In developer perspective

But if you are a developer, you might worry about Doze if it prevents your App from time critical jobs; for an example setting up an notification exactly at 4pm? No need to be upset, Android framework offers two ways to get important work done even when the device is Dozing!

  1. Using high priority messages in Google Cloud Messaging. High priority messages can wake your app if it’s in Doze mode.
  2. Use new alarm manager calls; setAndAllowWhileIdle(), setExactAndAllowWhileIdle() instead of set() and setExact(), for time critical tasks. Eg:- reminder App,

One restriction:- only can be triggered once every 15mins. This is actually reasonable for a device already in idle mode because if the user didn’t get the attention the first time, they probably don’t care two minutes later.

You can test your app’s behaviour in doze mode with below adb commands Google have introduced.

$ adb shell dumpsys battery unplug
$ adb shell dumpsys deviceidle force-idle

This force the system into doze mode. You can observe the behaviour playing with this.

To make it optimized for power usage of background tasks, Android wants developers to think on 3 main design principles.

  • Reduce: Reduce all background activity
  • Defer: If background activity must be performed, defer it to when device is on charging
  • Coalesce: If it cannot be deferred, coalesce it with other background activity to reduce wakeup overhead.

Now you see that Doze does Coalesce!!!

Doze != App Standby

Don’t confuse Doze with App Standby. It’s another feature introduced in Android Marshmallow to save battery. App Standby defers background activities for Apps the user is not actively using for days. The big picture is that, App-standby is mainly to save battery when the device is being used while Doze is to save battery when device is not being used.

Lightweight Doze

Android 7 and later extends Doze to a light mode. Every time a user turns off the screen, even when the user continues to move around, this “Lightweight Doze” mode activates.

Doze vs Lightweight Doze (image credits:- developers.android)

OK, now you know what makes your phone sleep like a baby!
If you find this interesting, make sure to give me some “claps” 👏👏👏 and show your support.

--

--

Malith Thilakarathne

#CSE @MoratuwaUni | #SocialMediaSquad @Microsoft | #Developer @moraSpirit | #Rtr @RotaractMora | www.malitht.com