blob: 75b72645282ad3105d6f1a525807efe80c87a982 [file] [log] [blame]
David Friedmanf3851c12016-07-20 13:44:10 -07001page.title=Optimizing Network Data Usage
2parent.title=Performing Network Operations
3parent.link=index.html
4
5trainingnavtop=true
6next.title=Parsing XML Data
7next.link=xml.html
8
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -08009@jd:body
10
David Friedmanf3851c12016-07-20 13:44:10 -070011<div id="tb-wrapper">
12<div id="tb">
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080013
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080014
Adarsh Fernando8cdd5e32016-03-14 13:49:12 -070015
David Friedmanf3851c12016-07-20 13:44:10 -070016<h2>This lesson teaches you to</h2>
17<ol>
18 <li><a href="#status">Check Data-Saver Preferences</a>
19 <ul>
20 <li><a href="#request-whitelist">Request whitelist permissions</a></li>
21 </ul>
22 </li>
23 <li><a href="#monitor-changes">Monitor Changes to Data Saver Preferences</a></li>
24 <li><a href="#testing">Test with Android Debug Bridge Commands</a></li>
25</ol>
26
27</div>
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080028</div>
29
30<p>
31 Over the life of a smartphone, the cost of a cellular data plan can easily
David Friedmanf3851c12016-07-20 13:44:10 -070032 exceed the cost of the device itself. From Android 7.0 (API level 24),
33 users users can enable Data Saver on a device-wide basis in order
34 optimize their device's data usage, and use less data. This ability
35 is especially useful when roaming, near the end of the billing cycle,
36 or for a small prepaid data pack.
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080037</p>
38
39<p>
40 When a user enables Data Saver in <strong>Settings</strong> and the device is
41 on a metered network, the system blocks background data usage and signals
42 apps to use less data in the foreground wherever possible. Users can
43 whitelist specific apps to allow background metered data usage even when Data
44 Saver is turned on.
45</p>
46
47<p>
Adarsh Fernando1c796b12016-03-08 17:01:36 -080048 The N Developer Preview extends the {@link android.net.ConnectivityManager}
49 API to provide apps with a way to <a href="#status">retrieve the user’s Data
50 Saver preferences</a> and <a href="#monitor-changes">monitor preference
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080051 changes</a>. It is considered good practice for apps to check whether the
52 user has enabled Data Saver and make an effort to limit foreground and
53 background data usage.
54</p>
55
Adarsh Fernando1c796b12016-03-08 17:01:36 -080056<h2 id="status">
57 Checking Data Saver Preferences
58</h2>
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080059
60<p>
Adarsh Fernando1c796b12016-03-08 17:01:36 -080061 In the N Developer Preview, apps can use the {@link
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -080062 android.net.ConnectivityManager} API to determine what data usage
63 restrictions are being applied. The {@code getRestrictBackgroundStatus()}
64 method returns one of the following values:
65</p>
66
67<dl>
68 <dt>
69 {@code RESTRICT_BACKGROUND_STATUS_DISABLED}
70 </dt>
71
72 <dd>
73 Data Saver is disabled.
74 </dd>
75
76 <dt>
77 {@code RESTRICT_BACKGROUND_STATUS_ENABLED}
78 </dt>
79
80 <dd>
81 The user has enabled Data Saver for this app. Apps should make an effort to limit data
82 usage in the foreground and gracefully handle restrictions to background
83 data usage.
84 </dd>
85
86 <dt>
87 {@code RESTRICT_BACKGROUND_STATUS_WHITELISTED}
88 </dt>
89
90 <dd>
91 The user has enabled Data Saver but the app is whitelisted. Apps should
92 still make an effort to limit foreground and background data usage.
93 </dd>
94</dl>
95
96<p>
97 It is considered good practice to limit data usage whenever the device is
98 connected to a metered network, even if Data Saver is disabled or the app
99 is whitelisted. The following sample code uses {@link
100 android.net.ConnectivityManager#isActiveNetworkMetered
101 ConnectivityManager.isActiveNetworkMetered()} and {@code
102 ConnectivityManager.getRestrictBackgroundStatus()} to determine how much data
103 the app should use:
104</p>
105
106<pre>
107ConnectivityManager connMgr = (ConnectivityManager)
108 getSystemService(Context.CONNECTIVITY_SERVICE);
109// Checks if the device is on a metered network
110if (connMgr.isActiveNetworkMetered()) {
111 // Checks user’s Data Saver settings.
Adarsh Fernando8cdd5e32016-03-14 13:49:12 -0700112 switch (connMgr.getRestrictBackgroundStatus()) {
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -0800113 case RESTRICT_BACKGROUND_STATUS_ENABLED:
114 // Background data usage is blocked for this app. Wherever possible,
115 // the app should also use less data in the foreground.
116
117 case RESTRICT_BACKGROUND_STATUS_WHITELISTED:
118 // The app is whitelisted. Wherever possible,
119 // the app should use less data in the foreground and background.
120
121 case RESTRICT_BACKGROUND_STATUS_DISABLED:
122 // Data Saver is disabled. Since the device is connected to a
123 // metered network, the app should use less data wherever possible.
124 }
125} else {
126 // The device is not on a metered network.
127 // Use data as required to perform syncs, downloads, and updates.
128}
129</pre>
130
Adarsh Fernando2ba8e2a2016-05-09 10:24:38 -0700131<h3 id="request-whitelist">
132 Requesting whitelist permissions
133</h3>
134
135<p>
136 If your app needs to use data in the background, it can request whitelist
137 permissions by sending a
138 <code>Settings.ACTION_IGNORE_BACKGROUND_DATA_RESTRICTIONS_SETTINGS</code>
Adarsh Fernandob1386a72016-05-12 15:18:14 -0700139 intent containing a URI of your app's package name: for example,
140 <code>package:MY_APP_ID</code>.
Adarsh Fernando2ba8e2a2016-05-09 10:24:38 -0700141</p>
142
143<p>
Adarsh Fernandob1386a72016-05-12 15:18:14 -0700144 Sending the intent and URI launches the <strong>Settings</strong> app and
145 displays data usage settings for your app. The user can then decide whether
146 to enable background data for your app. Before you send this intent, it is
147 good practice to first ask the user if they want to launch the
148 <strong>Settings</strong> app for the purpose of enabling background data
149 usage.
Adarsh Fernando2ba8e2a2016-05-09 10:24:38 -0700150</p>
151
Adarsh Fernando1c796b12016-03-08 17:01:36 -0800152<h2 id="monitor-changes">
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -0800153 Monitoring Changes to Data Saver Preferences
Adarsh Fernando1c796b12016-03-08 17:01:36 -0800154</h2>
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -0800155
156<p>
157 Apps can monitor changes to Data Saver preferences by creating a {@link
158 android.content.BroadcastReceiver} to listen for {@code
Adarsh Fernandob1386a72016-05-12 15:18:14 -0700159 ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED} and dynamically
160 registering the receiver with {@link android.content.Context#registerReceiver
Adarsh Fernandoebae30fc2016-03-01 11:09:49 -0800161 Context.registerReceiver()}. When an app receives this broadcast, it should
162 <a href="#status">check if the new Data Saver preferences affect its
163 permissions</a> by calling {@code
164 ConnectivityManager.getRestrictBackgroundStatus()}.
165</p>
166
167<p class="note">
168 <strong>Note:</strong> The system only sends this broadcast to apps that
169 dynamically register for them with {@link
170 android.content.Context#registerReceiver Context.registerReceiver()}. Apps
171 that register to receive this broadcast in their manifest will not receive
172 them.
David Friedmanbf31c622016-03-03 13:55:00 -0800173</p>
Adarsh Fernando8cdd5e32016-03-14 13:49:12 -0700174
175<h2 id="testing">
176 Testing with Android Debug Bridge Commands
177</h2>
178
Eric Schmidtb603eae2016-05-31 15:56:48 -0700179<p>
180 The <a href="{@docRoot}tools/help/adb.html">Android Debug Bridge (ADB)</a>
181 provides a few commands that you can use to test your app in Data Saver
182 conditions. You can check and configure network
183 permissions or set wireless networks as metered to test your app on unmetered
184 networks.
185</p>
Adarsh Fernando8cdd5e32016-03-14 13:49:12 -0700186<dl>
187 <dt>
188 <code>$ adb shell dumpsys netpolicy</code>
189 </dt>
190
191 <dd>
192 Generates a report that includes the current global background network
193 restriction setting, package UIDs currently on a whitelist, and the network
194 permissions of other known packages.
195 </dd>
196
197 <dt>
198 <code>$ adb shell cmd netpolicy</code>
199 </dt>
200
201 <dd>
202 Displays a full list of Network Policy Manager (netpolicy) commands.
203 </dd>
204
205 <dt>
206 <code>$ adb shell cmd netpolicy set restrict-background
207 &lt;boolean&gt;</code>
208 </dt>
209
210 <dd>
211 Enables or disables Data Saver mode when passing <code>true</code> or
212 <code>false</code>, respectively.
213 </dd>
214
215 <dt>
216 <code>$ adb shell cmd netpolicy add restrict-background-whitelist
217 &lt;UID&gt;</code>
218 </dt>
219
220 <dd>
221 Adds the specified package UID to the whitelist to allow background metered
222 data usage.
223 </dd>
224
225 <dt>
226 <code>$ adb shell cmd netpolicy remove restrict-background-whitelist
227 &lt;UID&gt;</code>
228 </dt>
229
230 <dd>
231 Removes the specified package UID from the whitelist to block background
232 metered data usage while Data Saver is enabled.
233 </dd>
Eric Schmidtb603eae2016-05-31 15:56:48 -0700234
235 <dt>
236 <code>$ adb shell cmd netpolicy list wifi-networks</code>
237 </dt>
238
239 <dd>
240 Lists all wifi networks, displaying whether they're metered.
241 </dd>
242
243
244 <dt>
245 <code>$ adb shell cmd netpolicy set metered-network &lt;WIFI_SSID&gt;
246 true</code>
247 </dt>
248
249 <dd>
250 Sets wifi with the specified SSID as metered, allowing you to simulate a
251 metered network on an unmetered network.
252 </dd>
David Friedmanf3851c12016-07-20 13:44:10 -0700253</dl>