-
Title:
Network & Battery Drain
-
Description:
-
Let's take a moment to make
something insanely clear.
-
As far as battery is concerned,
-
networking is the biggest, baddest,
dirtiest offender that there is.
-
Remember that inside of your phone
is a small piece of hardware that's
-
effectively a HAM radio,
its whole purpose it to communicate with
-
local cell phone towers and
transmit data to them at high volumes.
-
But the trick is that this chip,
isn't always active, you see,
-
once you send a packet of data the radio
chip will stay on for a certain amount
-
of time, just in case there's a response
from the server that it's expecting.
-
But if there's no activity, the hardware
will shut down to save battery life.
-
And as we've seen before, there's a
large battery spike when this chip first
-
turns on, and as long as it's
keeping itself awake to wait for
-
responses, it's going to keep on
draining the battery at the same time.
-
Now it's worth pointing out that there's
two primary ways in which most apps
-
interact with the radio.
-
Firstly, there are events
that need to occur right now.
-
These events are the result
of some user action, or
-
they arise from the immediate need
to update the UI of your app.
-
For example, imagine when the user
asks to load a new batch of tweets for
-
a trending hashtag,
-
since this is a user initiated action,
your app should respond ASAP.
-
On the other side of this coin are all
the networking jobs that don't need
-
to happen in a time critical manner for
example, uploading user data,
-
synchronizing background statistics, or
re-sizing all of your social photos.
-
So while the first set of tasks happen,
has to happen immediately,
-
in order to provide feedback to the
user, the second set of tasks can easily
-
be put off until later, when they can be
performed in a battery efficient manner.
-
And there's a high probability,
that the majority of your
-
network requests in your application
fall into this second category.
-
[LAUGH] Converting networking jobs
over to being more efficient is
-
a two step process.
-
Firstly, take a hard look at the mobile
radio row in your battery historian
-
tool for your application.
-
Each of those red bars that you see
here represent an active mobile radio,
-
any gaps between those bars
represent when the radio is asleep.
-
If you see lots of narrow bars and
-
gaps in your graph, this can point to
your performance problems, since it
-
means that you're churning through
lots of wake up and sleep cycles.
-
What you want instead, is to see large
gaps next to large blocks of activity.
-
This way you've reduced overhead by
minimizing the number of network
-
requests, and even better,
don't use the radio at all.
-
I mean you can wait until
the phone is connected to WiFi and
-
then let the WiFi hardware do all this
work with much less battery train.
-
Now, the problem is that writing
the code to batch, cache,
-
and defer all these networking requests
is really difficult to get right,
-
which is why we've done the work for
you.
-
The JobScheduler API that rolled out
with the L release of Android provides
-
a full suite of API's that do all of
this network request management work and
-
more, on your behalf.
-
But rather than telling you
about this wonderful API,
-
why don't you take a swing
at it in practice?