安卓(Android) 怎么exec new su shell命令

2019年10月13日15:23:29安卓(Android) 怎么exec new su shell命令已关闭评论 843 1135字阅读3分47秒

1.简介

安卓(Android) 怎么exec new su shell命令?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


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

public class Main {
    public static void execNewSUCommand(String command, boolean debug) {
        try {
            if (debug) {
                Log.d("SystemHelper", "Command: " + command);
            }
            Process process = new ProcessBuilder().command("su").start();
            process.getOutputStream().write(
                    (command + "\n").getBytes("ASCII"));
            process.getOutputStream().flush();
            if (debug) {
                StringBuffer sb = new StringBuffer();
                BufferedReader bre = new BufferedReader(
                        new InputStreamReader(process.getErrorStream()));
                Thread.sleep(10);
                while (bre.ready()) {
                    sb.append(bre.readLine());
                }
                String s = sb.toString();
                if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
                    Log.e("SystemHelper",
                            "Error with command: " + sb.toString());
                }
                sb = new StringBuffer();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(process.getInputStream()));
                Thread.sleep(10);
                while (br.ready()) {
                    sb.append(br.readLine());
                }
                s = sb.toString();
                if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
                    Log.e("SystemHelper", "Output from command: " + s);
                }
            }
            Thread.sleep(100);
        } catch (Exception e) {
            Log.e("SystemHelper", "Error executing: " + command, e);
        }
    }
}

 
继续阅读
编程十万个怎么办