2016 Google IO — 电量与内存优化

Battery Optimizations

Design Principles : Reduce , Defer , Coalesce (减少, 延迟, 合并)

Reduce —— reducing the background activity for an app when the user is not actively engaged, or is not actively using the device. We try and stay away from that urge, from that temptation, to push data onto our devices and keep them fresh even when the user is not actively using the app.(当用户没有活跃使用应用时,应尽量减少后台活动。用户没有活跃使用app时,我们不要一时冲动,要尝试着远离将数据推送到我们的设备和保持应用实时更新这种行为。)

Defer —— If background actvity must be performed, defer it to when device is on charger.Because the moment the device is plugged in, any activity that you do essentially becomes free.And the use does not incur any battery cost for it.(如果后台活动必须被执行,可以将后台活动推迟到设备充电的时候。因为这时候一切的活动变得非常自由。同时用户不会在乎电量的消耗成本。)

Coalesce —— If it cannot be defered, coalesce it with other background activity to reduce wakeup overhead.Batching and coalescing a lot of the background activity,
not just from your applications, but from other applications as well, in order to make the most efficient use of the CPU and the background networking traffic.Every time your device wakes up from suspend, or it activates the mobile radio to do any networking connectivity, be it for a very small amount of data packet,there is a fixed cost that needs to get incurred. Your devices goes into this high power state. And there is going to be a fixed overhead every time you wake up the device.(如果后台活动不能推迟,尽量和其他后台活动合并,减少唤醒的开销。批处理或者合并很多后台活动不仅仅局限于你自己的应用,也可以是其他的应用,以致于更有效使用CPU和后台网络流量。你的设备每一次的唤醒或者每一次的激活网络连接,是一个非常小的数据包,需要固定的开销。但你的设备进入高功率状态后,每一次唤醒将需要固定的开销。)

Doze and App Standby (瞌睡模式 - Starting from Android 6.0 - API level 23)

Doze —— Doze reduces battery consumption by deferring background CPU and network activity for apps when the device is unused for long periods of time.

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

In Doze mode, the system attempts to conserve battery by restricting apps’ access to network and CPU-intensive services. It also prevents apps from accessing the network and defers their jobs, syncs, and standard alarms. As soon as the user wakes the device by moving it, turning on the screen, or connecting a charger, the system exits Doze and all apps return to normal activity.

Other Use Cases

For example:

instant messaging app —— So for that use case, we, as part of Doze and App Standby, we launched something called a
high-priority Google Cloud Messaging message, which now is Firebase Cloud Messaging。

playing music —— We exempt foreground services from Doze and App standby. This is a service that has to have a persistent
notification. When enter Doze , this service will retain its network access and wakelock access.

alarm —— Regular AlarmManager alarms do not fire.So for those kinds of use cases, we launched new alarm APIs that will trigger in a limited way during Doze.

Summarization

  • High-Priority GCM (new FCM) messages
    • Grant apps temporary wakelock and network access
    • For use case requiring immediate notification to user
    • Normal FCM messages batched to maintenance windows
  • Foreground services
    • Exempt from Doze and App Standby
    • For use cases such as music playback
  • Alarm APIs
    • For alarms that need to trigger while devices is in Doze
  • Whitelist
    • Users can add app to whitelist via Settings
    • Apps can prompt users at runtime if they meet acceptable use case requirements app and pass Play Store review
Testing with Doze and App Standby

Your can Force the system to cycle through Doze modes by running the following commands. More

Marshmallow results
  • standby battery +2x
  • screen-off battery life + 30%

About Doze and App Standby More Detail

Memory Optimizations

Implicit Broadcast

Apps’ statically declared implicit broadcast receivers will no longer be woken up. Explicit broadcasts continue to work.

Broadcasts sent generally , not specific to a particular app. Any app can listen for them.
What makes this particularly bad is that often these broadcasts are listened to, the receivers for these broadcasts
are declared in a static way in the application’s manifest file. what that means is that the application doesn’t
actually have to be running for it to receive this broadcast and react to it. In fact, a broadcast sent in this manner
would cause the applications that are not even running, but listening for it, to be woken up, brought into memory, and
react to it in some way.

App Background services

Apps can no longer run unbound background services. They can still run foreground services for use cases such as music playback.

What change in N?
  • Apps targeting N will no longer be woken up as a result of these broadcast. receivers declared in manifest files For non runtime use cases, use network triggers in JobScheduler.

  • android.hardware.action.NEW_PICTURE
    android.hardware.action.NEW_VIDEO

    All apps (not just those targeting N-release) will no longer be able to send or receive these broadcasts.
    JobScheduler has been expanded to trigger on content provider changes.

APIS & Disagnostic tools

JobScheduler API (Starting from API 21 Lollipop)

  • wrapper around background activity
  • Schedule based on triggers:
    • Time window
    • Network type
    • Device charging
    • Device charging & unused
  • Platform optimizes for coalesce execution based on battery, connectivity conditions, and memory.

For API N

  • New: Trigger jobs on Content Provider updates
  • Prioritize jobs of foreground (or top) apps
  • Run more jobs concurrently and scale base on RAM availability

Example code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//1.upload to server every 24 hours
JobInfo uploadJob = new JobInfo.Builder(mSomeInt, mServiceComponent)
.setRequireNetworkCapabilities(JobInfo.NETWORK_TYPE_UNMETERED)
.setPeriodic(24 * DateUtils.HOURS_IN_MILLIS)
.setRequiresCharging(true)
.build();
mJobScheduler.schedule(uploadJob);

//2.Use JobScheduler instead of NEW_PICTURE
public static void scheduleJob(Content context){
JobScheduler js = content.getSystemService(JobScheduler.class);
JobInfo.Builder builder = new JobInfo.Builder(
R.id.schedule_photos_job,
new ComponentName(context, PhotoContentJob.class));
builder.addTriggerContentUri(
new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
js.schedule(builder.build());
}

Firebase JobDispatcher (For Android Pre-L)

  • New open-source SDK for scheduling and executing background activity on Android and soon , on iOS!
  • Mirrors most JobScheduler features for backwards compatibility.
  • JobDispatcher Github
Disagnostic tools —— Battery Historian tools

Get Battery Historian! github.com/google/battery-historian

更多 Google I/O 2016: Android 演讲视频汇总


Loading comments box needs to over the wall