run As Root in Shell - Android Android OS

Android examples for Android OS:Shell

Description

run As Root in Shell

Demo Code


//package com.java2s;

import android.util.Log;
import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static String TAG = "UM";

    public static void runAsRoot(String[] cmds) {
        Process p;/*from  w w w  .j  av a2 s .  c  o m*/
        try {
            p = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(p.getOutputStream());
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            for (String tmpCmd : cmds) {
                os.writeBytes(tmpCmd + "\n");
                String test;
                while ((test = bf.readLine()) != null) {
                    Log.i(TAG, test);
                }
            }

            //os.writeBytes("exit\n");
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials