2016-03-03 13:55:00 -08:00
|
|
|
|
page.title=Data Saver
|
2016-03-01 11:09:49 -08:00
|
|
|
|
metaDescription=User-enabled data usage optimization.
|
|
|
|
|
page.keywords="android N", "data usage", "metered network"
|
|
|
|
|
@jd:body
|
|
|
|
|
|
|
|
|
|
<div id="qv-wrapper">
|
|
|
|
|
<div id="qv">
|
|
|
|
|
<h2>
|
|
|
|
|
In this document
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
<ol>
|
|
|
|
|
<li>
|
2016-03-08 17:01:36 -08:00
|
|
|
|
<a href="#status">Checking Data Saver Preferences</a>
|
2016-03-01 11:09:49 -08:00
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>
|
2016-03-14 13:49:12 -07:00
|
|
|
|
<a href="#monitor-changes">Monitoring Changes to Data Saver Preferences</a>
|
|
|
|
|
</li>
|
|
|
|
|
|
|
|
|
|
<li>
|
|
|
|
|
<a href="#testing">Testing with Android Debug Bridge Commands</a>
|
2016-03-01 11:09:49 -08:00
|
|
|
|
</li>
|
|
|
|
|
</ol>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
Over the life of a smartphone, the cost of a cellular data plan can easily
|
2016-03-08 17:01:36 -08:00
|
|
|
|
exceed the cost of the device itself. In the N Developer Preview, users can
|
|
|
|
|
enable Data Saver on a device-wide basis in order to use less data, whether
|
|
|
|
|
roaming, near the end of the billing cycle, or on a small prepaid data pack.
|
2016-03-01 11:09:49 -08:00
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
When a user enables Data Saver in <strong>Settings</strong> and the device is
|
|
|
|
|
on a metered network, the system blocks background data usage and signals
|
|
|
|
|
apps to use less data in the foreground wherever possible. Users can
|
|
|
|
|
whitelist specific apps to allow background metered data usage even when Data
|
|
|
|
|
Saver is turned on.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p>
|
2016-03-08 17:01:36 -08:00
|
|
|
|
The N Developer Preview extends the {@link android.net.ConnectivityManager}
|
|
|
|
|
API to provide apps with a way to <a href="#status">retrieve the user’s Data
|
|
|
|
|
Saver preferences</a> and <a href="#monitor-changes">monitor preference
|
2016-03-01 11:09:49 -08:00
|
|
|
|
changes</a>. It is considered good practice for apps to check whether the
|
|
|
|
|
user has enabled Data Saver and make an effort to limit foreground and
|
|
|
|
|
background data usage.
|
|
|
|
|
</p>
|
|
|
|
|
|
2016-03-08 17:01:36 -08:00
|
|
|
|
<h2 id="status">
|
|
|
|
|
Checking Data Saver Preferences
|
|
|
|
|
</h2>
|
2016-03-01 11:09:49 -08:00
|
|
|
|
|
|
|
|
|
<p>
|
2016-03-08 17:01:36 -08:00
|
|
|
|
In the N Developer Preview, apps can use the {@link
|
2016-03-01 11:09:49 -08:00
|
|
|
|
android.net.ConnectivityManager} API to determine what data usage
|
|
|
|
|
restrictions are being applied. The {@code getRestrictBackgroundStatus()}
|
|
|
|
|
method returns one of the following values:
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<dl>
|
|
|
|
|
<dt>
|
|
|
|
|
{@code RESTRICT_BACKGROUND_STATUS_DISABLED}
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Data Saver is disabled.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
{@code RESTRICT_BACKGROUND_STATUS_ENABLED}
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
The user has enabled Data Saver for this app. Apps should make an effort to limit data
|
|
|
|
|
usage in the foreground and gracefully handle restrictions to background
|
|
|
|
|
data usage.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
{@code RESTRICT_BACKGROUND_STATUS_WHITELISTED}
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
The user has enabled Data Saver but the app is whitelisted. Apps should
|
|
|
|
|
still make an effort to limit foreground and background data usage.
|
|
|
|
|
</dd>
|
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
It is considered good practice to limit data usage whenever the device is
|
|
|
|
|
connected to a metered network, even if Data Saver is disabled or the app
|
|
|
|
|
is whitelisted. The following sample code uses {@link
|
|
|
|
|
android.net.ConnectivityManager#isActiveNetworkMetered
|
|
|
|
|
ConnectivityManager.isActiveNetworkMetered()} and {@code
|
|
|
|
|
ConnectivityManager.getRestrictBackgroundStatus()} to determine how much data
|
|
|
|
|
the app should use:
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
ConnectivityManager connMgr = (ConnectivityManager)
|
|
|
|
|
getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
// Checks if the device is on a metered network
|
|
|
|
|
if (connMgr.isActiveNetworkMetered()) {
|
|
|
|
|
// Checks user’s Data Saver settings.
|
2016-03-14 13:49:12 -07:00
|
|
|
|
switch (connMgr.getRestrictBackgroundStatus()) {
|
2016-03-01 11:09:49 -08:00
|
|
|
|
case RESTRICT_BACKGROUND_STATUS_ENABLED:
|
|
|
|
|
// Background data usage is blocked for this app. Wherever possible,
|
|
|
|
|
// the app should also use less data in the foreground.
|
|
|
|
|
|
|
|
|
|
case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
|
|
|
|
|
// The app is whitelisted. Wherever possible,
|
|
|
|
|
// the app should use less data in the foreground and background.
|
|
|
|
|
|
|
|
|
|
case RESTRICT_BACKGROUND_STATUS_DISABLED:
|
|
|
|
|
// Data Saver is disabled. Since the device is connected to a
|
|
|
|
|
// metered network, the app should use less data wherever possible.
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// The device is not on a metered network.
|
|
|
|
|
// Use data as required to perform syncs, downloads, and updates.
|
|
|
|
|
}
|
|
|
|
|
</pre>
|
|
|
|
|
|
2016-03-08 17:01:36 -08:00
|
|
|
|
<h2 id="monitor-changes">
|
2016-03-01 11:09:49 -08:00
|
|
|
|
Monitoring Changes to Data Saver Preferences
|
2016-03-08 17:01:36 -08:00
|
|
|
|
</h2>
|
2016-03-01 11:09:49 -08:00
|
|
|
|
|
|
|
|
|
<p>
|
|
|
|
|
Apps can monitor changes to Data Saver preferences by creating a {@link
|
|
|
|
|
android.content.BroadcastReceiver} to listen for {@code
|
|
|
|
|
ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED} ({@code
|
|
|
|
|
"android.net.conn.RESTRICT_BACKGROUND_CHANGED"}) and dynamically registering
|
|
|
|
|
the receiver with {@link android.content.Context#registerReceiver
|
|
|
|
|
Context.registerReceiver()}. When an app receives this broadcast, it should
|
|
|
|
|
<a href="#status">check if the new Data Saver preferences affect its
|
|
|
|
|
permissions</a> by calling {@code
|
|
|
|
|
ConnectivityManager.getRestrictBackgroundStatus()}.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<p class="note">
|
|
|
|
|
<strong>Note:</strong> The system only sends this broadcast to apps that
|
|
|
|
|
dynamically register for them with {@link
|
|
|
|
|
android.content.Context#registerReceiver Context.registerReceiver()}. Apps
|
|
|
|
|
that register to receive this broadcast in their manifest will not receive
|
|
|
|
|
them.
|
2016-03-03 13:55:00 -08:00
|
|
|
|
</p>
|
2016-03-14 13:49:12 -07:00
|
|
|
|
|
|
|
|
|
<h2 id="testing">
|
|
|
|
|
Testing with Android Debug Bridge Commands
|
|
|
|
|
</h2>
|
|
|
|
|
|
|
|
|
|
The <a href="{@docRoot}tools/help/adb.html">Android Debug Bridge (ADB)</a>
|
|
|
|
|
provides a few commands that you can use to check and configure network
|
|
|
|
|
permissions:
|
|
|
|
|
|
|
|
|
|
<dl>
|
|
|
|
|
<dt>
|
|
|
|
|
<code>$ adb shell dumpsys netpolicy</code>
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Generates a report that includes the current global background network
|
|
|
|
|
restriction setting, package UIDs currently on a whitelist, and the network
|
|
|
|
|
permissions of other known packages.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
<code>$ adb shell cmd netpolicy</code>
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Displays a full list of Network Policy Manager (netpolicy) commands.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
<code>$ adb shell cmd netpolicy set restrict-background
|
|
|
|
|
<boolean></code>
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Enables or disables Data Saver mode when passing <code>true</code> or
|
|
|
|
|
<code>false</code>, respectively.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
<code>$ adb shell cmd netpolicy add restrict-background-whitelist
|
|
|
|
|
<UID></code>
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Adds the specified package UID to the whitelist to allow background metered
|
|
|
|
|
data usage.
|
|
|
|
|
</dd>
|
|
|
|
|
|
|
|
|
|
<dt>
|
|
|
|
|
<code>$ adb shell cmd netpolicy remove restrict-background-whitelist
|
|
|
|
|
<UID></code>
|
|
|
|
|
</dt>
|
|
|
|
|
|
|
|
|
|
<dd>
|
|
|
|
|
Removes the specified package UID from the whitelist to block background
|
|
|
|
|
metered data usage while Data Saver is enabled.
|
|
|
|
|
</dd>
|
|
|
|
|
</dl>
|