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);
android android-5.0-lollipop
Ganesh AB - Android
source share