Programmatically switch mobile data to Android 4.4.2

I have always used this code to programmatically use mobile data:

ConnectivityManager conman = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); @SuppressWarnings("rawtypes") final Class conmanClass = Class.forName(conman.getClass().getName()); final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); iConnectivityManagerField.setAccessible(true); final Object iConnectivityManager = iConnectivityManagerField.get(conman); @SuppressWarnings("rawtypes") final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); @SuppressWarnings("unchecked") final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConnectivityManager, true); 

This worked well, except now on Android 4.4.2, where I get this exception:

 java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] at java.lang.Class.getConstructorOrMethod(Class.java:472) at java.lang.Class.getDeclaredMethod(Class.java:640) at com.test.auto3gPro.ClasseConnessione.settaConnessione(ClasseConnessione.java:48) at com.test.auto3gPro.receiver.ScreenBroadcastReceiver.onReceive(ScreenBroadcastReceiver.java:108) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5081) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method) 

Does anyone know how to fix this?

+9
android android-4.4-kitkat
Feb 02 '14 at 1:36 on
source share
4 answers

If you use cyanogenmod, the setMobileDataEnabled (boolean) method changes to setMobileDataEnabled (String, boolean) ... as you can see in this line of code .

Thus, you can use the standard method, and then in the catch block NoSuchMethodException try the "cyanogenmod" method as follows:

 Class[] cArg = new Class[2]; cArg[0] = String.class; cArg[1] = Boolean.TYPE; Method setMobileDataEnabledMethod; setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", cArg); Object[] pArg = new Object[2]; pArg[0] = getContext().getPackageName(); pArg[1] = true; setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConnectivityManager, pArg); 

I do not know if other mods are affected.

+9
Apr 18 '14 at 2:09
source share

This worked for me on Android 4.4.4 2

 public void onClick(View view){ ConnectivityManager dataManager; dataManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); Method dataMtd = null; try { dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } dataMtd.setAccessible(true); try { dataMtd.invoke(dataManager, true); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

Reflection still works on short release versions

+4
Sep 08 '14 at 11:00
source share

Looks like you found the danger of using reflection to play with inner classes. I am sure that this has not been shown, since the use of mobile data should be done by the user, not the application. If you really want to continue to do this, you will need to examine the new Android source files to find out the unexposed interfaces that you can detect at runtime, and to protect calls to this code with API level checks. I do not think that I recommend programmatically changing mobile data.

+2
Feb 02 '14 at 15:57
source share

setMobileDataEnabled removed in Android L.

Use this instead:

 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); Method methodSet = Class.forName(tm.getClass().getName()).getDeclaredMethod( "setDataEnabled", Boolean.TYPE); methodSet.invoke(tm,true); 

Make sure you have this permission on your manifest:

 <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/> 

This permission is for system applications only.

0
Jun 14 '16 at 8:51
source share



All Articles