Android 通过代码执行ADB命令

Android 实现
通过java 代码方式执行ADB命令

public String execShell(String cmd) {
        StringBuilder s = new StringBuilder();
        try {
            //su 为root用户   sh 为普通用户
            //java.lang.Process process = Runtime.getRuntime().exec("sh");
            //OutputStream outputStream = process.getOutputStream();
            //DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
            //写命令
            //dataOutputStream.writeBytes(cmd);
            //提交命令
            //dataOutputStream.flush();
            java.lang.Process process = Runtime.getRuntime().exec(cmd);
            InputStream inputStream = process.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(inputStream));
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                s.append(line).append("\n");
            }
            inputStream.close();
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s.toString();
    }

eg:

//获取当前进程的PID
int pid = Process.myPid();
//通过进程的PID列出该进程下所有的线程
String s = "ps -T -p " + pid;
Log.e("TAG", "  t... :" + execShell(s));
运行结果日志

就这样,可以通过adb 进行指定CPU...一系列操作...

你可能感兴趣的:(Android 通过代码执行ADB命令)