安卓(Android) 怎么执行命令su

2019年10月13日14:54:05安卓(Android) 怎么执行命令su已关闭评论 519 899字阅读2分59秒

1.简介

安卓(Android) 怎么执行命令su?——请看下文。

2.示例代码

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



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

import java.io.OutputStreamWriter;

public class Main {
    private final static String TAG = "readwhatsapp";
    private final static int COMMAND_ANSWER_WAIT_TIME = 15;

    public static boolean commandSU(String command) {
        FLOG(command);

        Runtime runtime = Runtime.getRuntime();
        Process proc = null;
        OutputStreamWriter osw = null;

        boolean ret = false;

        try {
            proc = runtime.exec("su");
            osw = new OutputStreamWriter(proc.getOutputStream());
            osw.write(command);
            osw.flush();
            osw.close();
            ret = true;
        } catch (Exception ex) {

        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {

                }
            }
        }

        try {
            if (proc != null) {
                int i = COMMAND_ANSWER_WAIT_TIME * 2;
                while (i-- > 0) {
                    try {
                        proc.exitValue();
                        break;
                    } catch (IllegalThreadStateException e) {
                    }
                    Thread.sleep(500);
                }
                if (i <= 0)
                    proc.destroy();
            }

        } catch (InterruptedException e) {
            ret = false;
        }
        return ret;
    }

    public static void FLOG(String s) {
        Log.v(TAG, s);
    }
}

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