How to disable Mobile Data on Android

Starting from ‘Gingerbread’ you can use the IConnectivityManager.setMobileDataEnabled() method.
It’s hidden in API, but can be accessed with reflection.
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/net/ConnectivityManager.java#376

With this method you can change the system setting:
‘Settings -> Wireless & network -> Mobile network settings -> Data Enabled

Code example:

private void setMobileDataEnabled(Context context, boolean enabled) {
    final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final Class conmanClass = Class.forName(conman.getClass().getName());
    final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
    iConnectivityManagerField.setAccessible(true);
    final Object iConnectivityManager = iConnectivityManagerField.get(conman);
    final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

Also you need the CHANGE_NETWORK_STATE permission.

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

Needless to say that this approach might not work in future android versions. But I guess that applications such as ‘3G watchdog’, ‘APNdroid’ or ‘DataLock’ work this way.


UPDATE:
setMobileDataEnabled method is no longer available on Lollipop

Leave a Comment