Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

Data Saver in Android N

In Android N, the user can turn on the option to save data in the settings.

Data Saver mode, a new system service that helps reduce cellular data use by apps, whether roaming, near the end of the billing cycle, or on a small prepaid data pack.


When a user enables Data Saver in Settings 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 white list specific apps to allow background metered data usage even when Data Saver is turned on.



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.
  switch (connMgr.getRestrictBackgroundStatus()) {
    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.
}
Apps can monitor changes to Data Saver preferences by creating a BroadcastReceiver to listen forConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED("android.net.conn.RESTRICT_BACKGROUND_CHANGED") and dynamically registering the receiver with Context.registerReceiver().

The system only sends this broadcast to apps that dynamically register for them with Context.registerReceiver(). Apps that register to receive this broadcast in their manifest will not receive them.


Test Data Saver options with ADB commands:

$ adb shell dumpsys netpolicy 

It displays the current global background network restriction setting, package UIDs currently on a whitelist, and the network permissions of other known packages.

$ adb shell cmd netpolicy

it displays netpolicy commands

$ adb shell cmd netpolicy set restrict-background <boolean>
Disable/Enable data saver option

$ adb shell cmd netpolicy add restrict-background-whitelist <UID>
Adds the specified package UID to the whitelist to allow background metered data usage.

$ adb shell cmd netpolicy remove restrict-background-whitelist <UID>
Removes the specified package UID from the whitelist to block background metered data usage while Data Saver is enabled.

No comments:

Post a Comment