a
554325746@qq.com
2019-12-25 38492bbaa63586e2f4877da0eaa01a082fd565a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.basic.security.utils;
 
public class LztekAndroidRoot {
    /* It is sample code for Lztek Android 7.1 and 8.1 root
 
     */
    public static void SafeClose(java.io.Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (java.io.IOException e) {
            }
        }
    }
 
    /* The command sequence is like "su root chmod 666 /dev/i2c-1".
     * Each command should be executed in this strange way :-)
     */
 
    public static String SuExec(String command, java.io.File workingDirectory) {
        if (null == command || (command = command.trim()).length() == 0) {
            return null;
        }
        if (null == workingDirectory) {
            workingDirectory = new java.io.File("/");
        }
        java.io.OutputStream out = null;
        java.io.InputStream in = null;
        java.io.InputStream err = null;
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("su root " + command, null, workingDirectory);
            StringBuffer inString = new StringBuffer();
            StringBuffer errString = new StringBuffer();
            out = process.getOutputStream();
 
            in = process.getInputStream();
            err = process.getErrorStream();
            while (in.available() > 0) {
                inString.append((char) in.read());
            }
            while (err.available() > 0) {
                errString.append((char) err.read());
            }
            return inString.toString();
        } catch (Exception ioex) {
            return null;
        } finally {
            SafeClose(out);
            SafeClose(in);
            SafeClose(err);
        }
    }
}