I am writing a little app that only I will use and I want to pragmatically enable / disable my mobile data on a rooted android 4.5 device (I am running a custom Android L fo
I've created this method looking around on internet; it works fine on rooted android 5.0.1 Basically you have 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();
}
}
Please report if this has problems on some device.
EDIT: Now also compatible with android 5.1 Credit
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);
}
}
Use this
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Method methodSet = tm.class.getDeclaredMethod( "setDataEnabled", boolean.class);
methodSet.invoke(tm, true);
Edit:
This requires permission MODIFY_PHONE_STATE
, this is System or signature level permission.
Ideally you could create a runnable jar file with this code and execute it using
export CLASSPATH=<jar path>
exec app_process <jar-dir-path> your.package.name.classname "$@"
from su shell.
I noticed that the service call method does not work consistently on all devices. The number to be used in it varied from device to device.
I have found the following solution which works without any issue across all ROOTED devices.
Simply execute the following via su
To enable mobile data
svc data enable
To disable mobile data
svc data disable
It's as simple as that.