Search code examples
androidconnectionsu

Is possible turn on/off data connection in Android Lollipop rooted?


I'm creating an app that needs to change the data connection. I found a solution: using su commands, but the problem is that Toast Warning shows every time when I execute the command.... Is possible using these commands without toast warning ?
Or
Is there a way to toggle the data connection enabled with TelephonyManager using reflections? I tried it, but it didn't works.

My code is below:

public static void setMobileDataState(boolean mMobileDataEnabled){

    try{
        if(mMobileDataEnabled)
            Shell.runAsRoot(new String[]{"svc data enable"});
        else
            Shell.runAsRoot(new String[]{"svc data disable"});
    }
    catch (Exception ex){
        Utilities.log(ex.toString());
    }

}



public class Shell {

public static void runAsRoot(String[] mCommands){

    try {
        Process mProcess = Runtime.getRuntime().exec("su");
        DataOutputStream mOS = new DataOutputStream(mProcess.getOutputStream());
        for (String mCommand : mCommands) {
            mOS.writeBytes(mCommand + "\n");
        }
        mOS.writeBytes("exit\n");
        mOS.flush();

    }catch (Exception o){
        Utilities.log(o.toString());
    }

}
}

Solution

  • I found the solution..... I did the following:

    1. I rooted my device
    2. I installed my app as system app, it's simple, just copy your apk into /system/priv-app/myApk.apk and set chmod 644 permissions. If you have a doubt, check this post (If i set my android app to a system app, after a factory reset, will it be removed from the phone?).
    3. I just removed the /system/app/SuperSU folder
    4. I did a factory reset on device, and that is it ..... =D