How to disable Mobile Data on Android
Solution 1:
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
Solution 2:
The Dataconnection disable and enabling APIS are hidden in the SDK and not exposed to the user, this can be achived by accessing the ITelephony interface using the java reflection technique.
here you go:
Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
isEnabled = true;
}else{
isEnabled = false;
}
telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
if (isEnabled) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
Solution 3:
Switching the mobile data net connectivity by changing the APN's name doesn't work well any more since Gingerbread. And while the reflection code is probably the right way to do the trick, it won't work, because the application needs the android.permission.MODIFY_PHONE_STATE permission as explained by Alex P. Otherwise you get this nasty exception:
03-18 21:54:55.074: WARN/System.err(1851): java.lang.reflect.InvocationTargetException
(...)
03-18 21:54:55.263: WARN/System.err(1851): Caused by: java.lang.SecurityException: Neither user 10037 nor current process has android.permission.MODIFY_PHONE_STATE.
(...)
03-18 21:54:55.303: WARN/System.err(1851): at com.android.internal.telephony.ITelephony$Stub$Proxy.disableDataConnectivity(ITelephony.java:888)
Unfortunately, you cannot set this permission, because it is a level 3 permission not allowed for applications:
03-18 21:48:39.334: WARN/PackageManager(75): Not granting permission android.permission.MODIFY_PHONE_STATE to package XXX (protectionLevel=3 flags=0x8be46)
I don't suppose anyone has a way to override the permission grant suppression other than by using own firmware.
Solution 4:
I think there are two primary types of mobile data connection on an android device: WiFi and 3G/HSDPA/etc.
And afaik you should be able to disable WiFi programmatically, but I think the 3G/HSDPA/etc connections can only be disabled by changing the APN's name. The reason I'm saying this is because the popular application APNDroid does it that way.
Solution 5:
Notice that "android.permission.MODIFY_PHONE_STATE" is no longer supported for Android 2.3 and up.
why 2.3 version of android does not hava android.permission.MODIFY_PHONE_STATE ? and what is the solution for this?