Last active
October 7, 2024 09:26
-
-
Save melihmucuk/6817536 to your computer and use it in GitHub Desktop.
Programmatically on off mobile data on android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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.
this error also shown for me:
error turning on/off datajava.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
Does this only work before lollipop ?
Yes, this only works for android version before lollipop. That's when they removed the method.
Ok
Damn, Google keep deprecating a lot of useful stuff.
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
This didn't work for me, throwing the following exception:
E/Mobile Data: error turning on/off datajava.lang.NoSuchMethodException: setMobileDataEnabled [boolean]