I need to Turn ON/OFF Mobile data programmatically. Below code is not working for 5.x. Can you please help me. Thanks in advance.
private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
connectivityManagerField.setAccessible(true);
final Object connectivityManager = connectivityManagerField.get(conman);
final Class connectivityManagerClass = Class.forName(connectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(connectivityManager, enabled); }
03-30 12:42:29.466: W/System.err(5966): java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] 03-30 12:42:29.466: W/System.err(5966): at java.lang.Class.getMethod(Class.java:664) 03-30 12:42:29.466: W/System.err(5966): at java.lang.Class.getDeclaredMethod(Class.java:626)
java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] @ below line.
final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
For your find Information, unless you have a rooted phone I don't think you can enable and disable data programmatically because in order to do so we have to include MODIFY_PHONE_STATE permission which is only given to system or signature apps. setMobileDataEnabled() method is no longer callable via reflection.
It seems like the setMobileDataEnabled method no longer exists in ConnectivityManager and this functionality was moved to TelephonyManager with two methods getDataEnabled and setDataEnabled.
public void setMobileDataState(boolean mobileDataEnabled)
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method setMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("setDataEnabled", boolean.class);
if (null != setMobileDataEnabledMethod)
{
setMobileDataEnabledMethod.invoke(telephonyService, mobileDataEnabled);
}
}
catch (Exception ex)
{
Log.e(TAG, "Error setting mobile data state", ex);
}
}
public boolean getMobileDataState()
{
try
{
TelephonyManager telephonyService = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Method getMobileDataEnabledMethod = telephonyService.getClass().getDeclaredMethod("getDataEnabled");
if (null != getMobileDataEnabledMethod)
{
boolean mobileDataEnabled = (Boolean) getMobileDataEnabledMethod.invoke(telephonyService);
return mobileDataEnabled;
}
}
catch (Exception ex)
{
Log.e(TAG, "Error getting mobile data state", ex);
}
return false;
}
When executing the code you get a SecurityException stating that Neither user 10089 nor current process has android.permission.MODIFY_PHONE_STATE.
A permission MODIFY_PHONE_STATE should be added I got this from Answer Thank you Muzikant
In Android L 5.xx the hidden API setMobileDataEnabled method is removed and it can no longer be used. You can verify this in android lolipop source code under /frameworks/base/core/java/android/net/ConnectivityManager.java.
If you still insist to perform it, you can use code snippet answered by Kushal but getDataEnabled is a system api, which normal user applications cant access. There is also one more system api available setDataEnabled under TelephonyManager. (/frameworks/base/telephony/java/android/telephony/TelephonyManager.java)
/** @hide */
@SystemApi
public void setDataEnabled(boolean enable) {
setDataEnabled(SubscriptionManager.getDefaultDataSubId(), enable);
}
It also needs the permission "android.permission.MODIFY_PHONE_STATE" which will work only on rooted devices.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With