Skip to content

Instantly share code, notes, and snippets.

@melihmucuk
Last active May 21, 2021 00:55
Show Gist options
  • Save melihmucuk/6817536 to your computer and use it in GitHub Desktop.
Save melihmucuk/6817536 to your computer and use it in GitHub Desktop.
Programmatically on off mobile data on android
boolean DataOnOff(boolean status, Context context) {
int bv = 0;
try {
if (bv == Build.VERSION_CODES.FROYO){
//android 2.2 versiyonu için
Method dataConnSwitchmethod;
Class<?> telephonyManagerClass;
Object ITelephonyStub;
Class<?> ITelephonyClass;
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
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 (status) {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("enableDataConnectivity");
} else {
dataConnSwitchmethod = ITelephonyClass
.getDeclaredMethod("disableDataConnectivity");
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
} else {
// android 2.2 üstü versiyonlar için
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, status);
}
return true;
} catch (Exception e) {
Log.e("Mobile Data", "error turning on/off data");
return false;
}
}
@ahmedbr
Copy link

ahmedbr commented Aug 6, 2018

This didn't work for me, throwing the following exception:

E/Mobile Data: error turning on/off datajava.lang.NoSuchMethodException: setMobileDataEnabled [boolean]

@rudreshrm05
Copy link

Even though the methods are hidden, this is being done using reflection. What google did is they removed the methods to turn on/off mobile data from android lollipop and later.

@fazurullah
Copy link

this error also shown for me:

error turning on/off datajava.lang.NoSuchMethodException: setMobileDataEnabled [boolean]

@JohnDotOwl
Copy link

Does this only work before lollipop ?

@Regenhardt
Copy link

Yes, this only works for android version before lollipop. That's when they removed the method.

@Elexy101
Copy link

Ok

@Borwe
Copy link

Borwe commented Oct 23, 2020

Damn, Google keep deprecating a lot of useful stuff.

@harendraskd
Copy link

anyone have solutions for android 10 and 11 (automatic turn on/off mobile data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment