android3g4g

How can I turn off/on 3G/4G/Data programmatically on Android 5.0?


Why does this code not work on android 5.0 ?

I testing on android 4.3, working but on android 5.0 not working.

please help me.

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 java.lang.reflect.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);
	}


Solution

  • Google has called off their API for data on/off pragmatically. You can check the issue filed on link.

    If you want to know some more details long answer is on this link where detailed discussion is there

    and if you find this helpful accept this as an answer