Execute terminal commands on rooted device - Android Android OS

Android examples for Android OS:Root

Description

Execute terminal commands on rooted device

Demo Code


import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import com.imalur.battery100discharger.R;
import android.content.Context;
import android.widget.Toast;

public class Main{
    /**/* ww w  .j a va 2  s .com*/
     * Execute terminal commands on rooted device
     * @param cmds
     * @throws IOException
     */
    public static void runAsRoot(String[] cmds) throws IOException {
        Process p = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
    }
}

Related Tutorials