Android applications that depend on background network requests—such as real-time messaging sync, media downloads, and telemetry beacons—must respect mobile data constraints configured by the user. When a user turns on Data Saver in Android settings or connects to a network marked as metered, the operating system’s Network Policy Manager (netpolicy) enforces kernel-level eBPF/iptables firewall rules that drop all background network packets from non-whitelisted apps. Because toggling system settings manually slows down test automation, Android Debug Bridge (ADB) exposes the cmd netpolicy interface to simulate restricted states instantly.
In this guide, you will learn how to toggle Data Saver via ADB, manage background restriction whitelists and blacklists, simulate metered network states, and audit network firewall rules on Android.
Understanding Android Network Policy and Data Saver
The Android Network Policy service regulates internet access based on three intersecting criteria:
- Data Saver (Global Background Restriction): When active, apps running in the background cannot transmit or receive packets on metered connections unless granted unrestricted data access.
- Per-App Background Data Disallowance: Users can manually toggle “Background data” off for individual apps in App Info, cutting off background data entirely even on unmetered Wi-Fi.
- Metered Networks: Cellular data is metered by default, but users can also designate specific Wi-Fi SSIDs as metered networks.
When an app attempts a network socket connection while restricted, the Linux kernel returns a socket error (such as EPERM or NetworkErrorException). Testing this ensures apps fail gracefully without crashing.
Retrieving Application UID
Network policy rules in Android are enforced per Linux User Identifier (UID) rather than package names alone. Before configuring restrictions, determine your application’s UID:
adb shell pm list packages -U | grep "com.example.myapp"
The output returns the package alongside its integer UID (for example, package:com.example.myapp uid:10245). Note this UID for firewall rule commands.
Enabling and Disabling Data Saver via ADB
To simulate Data Saver mode across the entire device using the Network Policy command-line interface:
# Enable global background data restriction (Data Saver ON)
adb shell cmd netpolicy set restrict-background true
# Disable global background data restriction (Data Saver OFF)
adb shell cmd netpolicy set restrict-background false
Confirm the current restriction state:
adb shell cmd netpolicy get restrict-background
The command outputs true or false, matching the visual Data Saver icon in the Android status bar.
Managing the Unrestricted Data Access Whitelist
When Data Saver is active, enterprise applications or critical VoIP services may require unrestricted background data access. You can add or remove an app from the Data Saver whitelist via ADB:
# Add application UID to Data Saver whitelist
adb shell cmd netpolicy add restrict-background-whitelist 10245
# Remove application UID from whitelist
adb shell cmd netpolicy remove restrict-background-whitelist 10245
To inspect all currently whitelisted application UIDs:
adb shell cmd netpolicy list restrict-background-whitelist
Simulating Per-App Background Blacklists
To test how your application behaves when a user specifically navigates to App Info > Mobile data & Wi-Fi and disables Background data:
# Add app to background restriction blacklist
adb shell cmd netpolicy add restrict-background-blacklist 10245
# Remove app from blacklist
adb shell cmd netpolicy remove restrict-background-blacklist 10245
List all blacklisted applications:
adb shell cmd netpolicy list restrict-background-blacklist
Simulating Metered Wi-Fi Networks via ADB
By default, Wi-Fi connections are treated as unmetered, bypassing Data Saver limits. To test whether your application correctly respects ConnectivityManager.isActiveNetworkMetered() on Wi-Fi without switching to cellular data:
- Identify the active Wi-Fi network interface (usually
wlan0):adb shell ip route - Mark the active Wi-Fi network as metered:
adb shell cmd netpolicy set metered-network wlan0 true - Confirm the network policy setting:
adb shell dumpsys netpolicy | grep -A 5 "Metered networks:"
Now, while connected to Wi-Fi, Android treats the connection as a metered cellular link, applying all Data Saver rules immediately.
Auditing Network Firewall Rules via Dumpsys
To verify that the underlying Linux kernel firewall has applied the restriction to your app’s UID, query dumpsys netpolicy:
adb shell dumpsys netpolicy | grep -A 10 "UID=10245"
Look for the policy and firewallRule attributes:
policy=0x1: Indicates background data is blocked.firewallRule=BLOCKED: Packets emitted from this UID are dropped at the socket layer.
Monitor your application’s logcat stream to verify that network calls catch IOException or check getActiveNetworkInfo().isMetered() before initiating large background syncs:
adb logcat -s "MyNetworkClient" "System.err"
Once testing is concluded, restore normal network operation:
adb shell cmd netpolicy set restrict-background false
adb shell cmd netpolicy remove restrict-background-blacklist 10245
adb shell cmd netpolicy remove restrict-background-whitelist 10245