Disable / Enable mobile data on Android L with root access

I am writing a small application that I will only use, and I want to pragmatically enable / disable my mobile data on the Android 4.5 root device (I am running a custom Android L for Nexus 4).

I searched for a while, and I found reflection methods that worked before Android 4.3. I also saw a method from this post Programmatically translating mobile data to Android 4.4.2 , but this requires cyanogenmod.

From what I can find on the Internet, this is not possible for non-root applications, but my question is:

is there something i can do with my root privileges to accomplish this?

+12
android android-5.0-lollipop
Aug 23 '14 at 21:05
source share
4 answers

I created this method, looking around the internet; it works fine on root android 5.0.1 Basically you need to pass true if you want the connection to be enabled, false otherwise and the context of your application.

private final static String COMMAND_L_ON = "svc data enable\n "; private final static String COMMAND_L_OFF = "svc data disable\n "; private final static String COMMAND_SU = "su"; public static void setConnection(boolean enable,Context context){ String command; if(enable) command = COMMAND_L_ON; else command = COMMAND_L_OFF; try{ Process su = Runtime.getRuntime().exec(COMMAND_SU); DataOutputStream outputStream = new DataOutputStream(su.getOutputStream()); outputStream.writeBytes(command); outputStream.flush(); outputStream.writeBytes("exit\n"); outputStream.flush(); try { su.waitFor(); } catch (InterruptedException e) { e.printStackTrace(); } outputStream.close(); }catch(IOException e){ e.printStackTrace(); } } 

Report if there is a problem on any device.

EDIT : now also compatible with android 5.1 Credit

+9
Jan 12 '15 at 19:07
source share

Use this

 TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); Method methodSet = tm.class.getDeclaredMethod( "setDataEnabled", boolean.class); methodSet.invoke(tm, true); 

Edit: This requires MODIFY_PHONE_STATE permission, this permission at the system level or subscription level.

Ideally, you can create a jar executable with this code and execute it with

 export CLASSPATH=<jar path> exec app_process <jar-dir-path> your.package.name.classname "$@" 

from su shell.

+2
Oct 13 '14 at 4:24
source share

I noticed that the service call method does not work consistently on all devices. The number that will be used in it varies from device to device.

I found the following solution that works without any problems on all ROOTED devices.

Just do the following via su

To enable mobile data

 svc data enable 

To disable mobile data

 svc data disable 

It is so simple.

+2
Apr 3 '15 at 21:50
source share
 void turnData(boolean ON) throws Exception { int currentapiVersion = android.os.Build.VERSION.SDK_INT; if(currentapiVersion == Build.VERSION_CODES.FROYO) { Log.i("version:", "Found Froyo"); try{ Method dataConnSwitchmethod; Class telephonyManagerClass; Object ITelephonyStub; Class ITelephonyClass; TelephonyManager telephonyManager = (TelephonyManager) cx.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); telephonyManagerClass = Class.forName(telephonyManager.getClass().getName()); Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony"); getITelephonyMethod.setAccessible(true); ITelephonyStub = getITelephonyMethod.invoke(telephonyManager); ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName()); if (ON) { dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("enableDataConnectivity"); } else { dataConnSwitchmethod = ITelephonyClass.getDeclaredMethod("disableDataConnectivity"); } dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub); }catch(Exception e){ Log.e("Error:",e.toString()); } } else { Log.i("version:", "Found Gingerbread+"); final ConnectivityManager conman = (ConnectivityManager) cx.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); final Class conmanClass = Class.forName(conman.getClass().getName()); final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); iConnectivityManagerField.setAccessible(true); final Object iConnectivityManager = iConnectivityManagerField.get(conman); final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConnectivityManager, ON); } } 
0
Jan 08 '15 at 10:47
source share



All Articles