• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Data Saver
2metaDescription=User-enabled data usage optimization.
3page.keywords="android N", "data usage", "metered network"
4page.image=images/cards/card-nyc_2x.jpg
5@jd:body
6
7<div id="qv-wrapper">
8  <div id="qv">
9    <h2>
10      In this document
11    </h2>
12
13    <ol>
14      <li>
15        <a href="#status">Checking Data Saver Preferences</a>
16        <ol>
17          <li>
18            <a href="#request-whitelist">Requesting whitelist permissions</a>
19          </li>
20        </ol>
21      </li>
22
23      <li>
24        <a href="#monitor-changes">Monitoring Changes to Data Saver
25        Preferences</a>
26      </li>
27
28      <li>
29        <a href="#testing">Testing with Android Debug Bridge Commands</a>
30      </li>
31    </ol>
32  </div>
33</div>
34
35<p>
36  Over the life of a smartphone, the cost of a cellular data plan can easily
37  exceed the cost of the device itself. In the N Developer Preview, users can
38  enable Data Saver on a device-wide basis in order to use less data, whether
39  roaming, near the end of the billing cycle, or on a small prepaid data pack.
40</p>
41
42<p>
43  When a user enables Data Saver in <strong>Settings</strong> and the device is
44  on a metered network, the system blocks background data usage and signals
45  apps to use less data in the foreground wherever possible. Users can
46  whitelist specific apps to allow background metered data usage even when Data
47  Saver is turned on.
48</p>
49
50<p>
51  The N Developer Preview extends the {@link android.net.ConnectivityManager}
52  API to provide apps with a way to <a href="#status">retrieve the user’s Data
53  Saver preferences</a> and <a href="#monitor-changes">monitor preference
54  changes</a>. It is considered good practice for apps to check whether the
55  user has enabled Data Saver and make an effort to limit foreground and
56  background data usage.
57</p>
58
59<h2 id="status">
60  Checking Data Saver Preferences
61</h2>
62
63<p>
64  In the N Developer Preview, apps can use the {@link
65  android.net.ConnectivityManager} API to determine what data usage
66  restrictions are being applied. The {@code getRestrictBackgroundStatus()}
67  method returns one of the following values:
68</p>
69
70<dl>
71  <dt>
72    {@code RESTRICT_BACKGROUND_STATUS_DISABLED}
73  </dt>
74
75  <dd>
76    Data Saver is disabled.
77  </dd>
78
79  <dt>
80    {@code RESTRICT_BACKGROUND_STATUS_ENABLED}
81  </dt>
82
83  <dd>
84    The user has enabled Data Saver for this app. Apps should make an effort to limit data
85    usage in the foreground and gracefully handle restrictions to background
86    data usage.
87  </dd>
88
89  <dt>
90    {@code RESTRICT_BACKGROUND_STATUS_WHITELISTED}
91  </dt>
92
93  <dd>
94    The user has enabled Data Saver but the app is whitelisted. Apps should
95    still make an effort to limit foreground and background data usage.
96  </dd>
97</dl>
98
99<p>
100  It is considered good practice to limit data usage whenever the device is
101  connected to a metered network, even if Data Saver is disabled or the app
102  is whitelisted. The following sample code uses {@link
103  android.net.ConnectivityManager#isActiveNetworkMetered
104  ConnectivityManager.isActiveNetworkMetered()} and {@code
105  ConnectivityManager.getRestrictBackgroundStatus()} to determine how much data
106  the app should use:
107</p>
108
109<pre>
110ConnectivityManager connMgr = (ConnectivityManager)
111        getSystemService(Context.CONNECTIVITY_SERVICE);
112// Checks if the device is on a metered network
113if (connMgr.isActiveNetworkMetered()) {
114  // Checks user’s Data Saver settings.
115  switch (connMgr.getRestrictBackgroundStatus()) {
116    case RESTRICT_BACKGROUND_STATUS_ENABLED:
117    // Background data usage is blocked for this app. Wherever possible,
118    // the app should also use less data in the foreground.
119
120    case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
121    // The app is whitelisted. Wherever possible,
122    // the app should use less data in the foreground and background.
123
124    case RESTRICT_BACKGROUND_STATUS_DISABLED:
125    // Data Saver is disabled. Since the device is connected to a
126    // metered network, the app should use less data wherever possible.
127  }
128} else {
129  // The device is not on a metered network.
130  // Use data as required to perform syncs, downloads, and updates.
131}
132</pre>
133
134<h3 id="request-whitelist">
135  Requesting whitelist permissions
136</h3>
137
138<p>
139  If your app needs to use data in the background, it can request whitelist
140  permissions by sending a
141  <code>Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS</code>
142  intent containing a URI of your app's package name: for example,
143  <code>package:MY_APP_ID</code>.
144</p>
145
146<p>
147  Sending the intent and URI launches the <strong>Settings</strong> app and
148  displays data usage settings for your app. The user can then decide whether
149  to enable background data for your app. Before you send this intent, it is
150  good practice to first ask the user if they want to launch the
151  <strong>Settings</strong> app for the purpose of enabling background data
152  usage.
153</p>
154
155<h2 id="monitor-changes">
156  Monitoring Changes to Data Saver Preferences
157</h2>
158
159<p>
160  Apps can monitor changes to Data Saver preferences by creating a {@link
161  android.content.BroadcastReceiver} to listen for {@code
162  ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED} and dynamically
163  registering the receiver with {@link android.content.Context#registerReceiver
164  Context.registerReceiver()}. When an app receives this broadcast, it should
165  <a href="#status">check if the new Data Saver preferences affect its
166  permissions</a> by calling {@code
167  ConnectivityManager.getRestrictBackgroundStatus()}.
168</p>
169
170<p class="note">
171  <strong>Note:</strong> The system only sends this broadcast to apps that
172  dynamically register for them with {@link
173  android.content.Context#registerReceiver Context.registerReceiver()}. Apps
174  that register to receive this broadcast in their manifest will not receive
175  them.
176</p>
177
178<h2 id="testing">
179  Testing with Android Debug Bridge Commands
180</h2>
181
182<p>
183  The <a href="{@docRoot}tools/help/adb.html">Android Debug Bridge (ADB)</a>
184  provides a few commands that you can use to test your app in Data Saver
185  conditions. You can check and configure network
186  permissions or set wireless networks as metered to test your app on unmetered
187  networks.
188</p>
189<dl>
190  <dt>
191    <code>$ adb shell dumpsys netpolicy</code>
192  </dt>
193
194  <dd>
195    Generates a report that includes the current global background network
196    restriction setting, package UIDs currently on a whitelist, and the network
197    permissions of other known packages.
198  </dd>
199
200  <dt>
201    <code>$ adb shell cmd netpolicy</code>
202  </dt>
203
204  <dd>
205    Displays a full list of Network Policy Manager (netpolicy) commands.
206  </dd>
207
208  <dt>
209    <code>$ adb shell cmd netpolicy set restrict-background
210    &lt;boolean&gt;</code>
211  </dt>
212
213  <dd>
214    Enables or disables Data Saver mode when passing <code>true</code> or
215    <code>false</code>, respectively.
216  </dd>
217
218  <dt>
219    <code>$ adb shell cmd netpolicy add restrict-background-whitelist
220    &lt;UID&gt;</code>
221  </dt>
222
223  <dd>
224    Adds the specified package UID to the whitelist to allow background metered
225    data usage.
226  </dd>
227
228  <dt>
229    <code>$ adb shell cmd netpolicy remove restrict-background-whitelist
230    &lt;UID&gt;</code>
231  </dt>
232
233  <dd>
234    Removes the specified package UID from the whitelist to block background
235    metered data usage while Data Saver is enabled.
236  </dd>
237
238  <dt>
239    <code>$ adb shell cmd netpolicy list wifi-networks</code>
240  </dt>
241
242  <dd>
243    Lists all wifi networks, displaying whether they're metered.
244  </dd>
245
246
247  <dt>
248    <code>$  adb shell cmd netpolicy set metered-network &lt;WIFI_SSID&gt;
249      true</code>
250  </dt>
251
252  <dd>
253    Sets wifi with the specified SSID as metered, allowing you to simulate a
254    metered network on an unmetered network.
255  </dd>
256</dl>