Android L (5.x) Turns Mobile Data on / off programmatically

I need to enable / disable mobile data programmatically. Below code does not work for 5.x. Could you 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): with java.lang .Class.getMethod (Class.java:664) 03-30 12: 42: 29.466: W / System.err (5966): when java.lang.Class.getDeclaredMethod (Class.java:626)

java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] @ below the line.

final method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod ("setMobileDataEnabled", Boolean.TYPE);

+7
android android-5.0-lollipop
source share
2 answers

It seems that the setMobileDataEnabled method no longer exists in ConnectivityManager , and this function has been ported 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 you execute the code, you get a SecurityException message that says that neither user 10089 nor the current process have android.permission.MODIFY_PHONE_STATE.

Permission must be added MODIFY_PHONE_STATE I got this from Reply Thanks Muzikant

+4
source share

In Android L 5.xx, the hidden setMobileDataEnabled API is removed and can no longer be used. You can check this in the lolipop android source code in /frameworks/base/core/java/android/net/ConnectivityManager.java.

If you still insist on executing it, you can use a code snippet that responded to Kushal, but getDataEnabled is a system api that may not be accessible to regular user applications. TelephonyManager has another api system setDataEnabled . (/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 only work on root devices.

+1
source share

All Articles