-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndRunShell.java
More file actions
123 lines (104 loc) · 3.61 KB
/
Copy pathAndRunShell.java
File metadata and controls
123 lines (104 loc) · 3.61 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package com.linyb;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by lybly on 2018/2/24.
*/
public class AndRunShell {
private final static String Test_su = "test su";
public static DataInputStream suTerminal(String command) {
Process process = null;
try {
process = Runtime.getRuntime().exec("su");
} catch (IOException e) {
e.printStackTrace();
}
//执行到这,Superuser会跳出来,选择是否允许获取最高权限
OutputStream outstream = process.getOutputStream();
DataOutputStream DOPS = new DataOutputStream(outstream);
InputStream instream = process.getInputStream();
DataInputStream DIPS = new DataInputStream(instream);
String temp = command + "\n";
//加回车
try {
DOPS.writeBytes(temp);
} catch (IOException e) {
e.printStackTrace();
}
//执行
try {
DOPS.flush();
} catch (IOException e) {
e.printStackTrace();
}
//刷新,确保都发送到outputstream
try {
DOPS.writeBytes("exit\n");
} catch (IOException e) {
e.printStackTrace();
}
//退出
try {
DOPS.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
return DIPS;
}
public static boolean isRooted() {
//检测是否ROOT过
DataInputStream stream;
boolean flag = false;
try {
stream = suTerminal("ls /");
//目录哪都行,不一定要需要ROOT权限的
if (stream.readLine() != null) flag = true;
//根据是否有返回来判断是否有root权限
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return flag;
}
//如shell一样执行命令
public static void nosuTerminal(final Context context, final String shellstr) {
//Thread为子线程,进行耗时操作
final Activity activityincome = (Activity) context;
new Thread(new Runnable() {
@Override
public void run() {
Activity activity = (Activity) context;
try {
//可以直接使用 su -c来执行内容
//Process process = Runtime.getRuntime().exec("su -c am start com.android.browser/.BrowserPreferencesPage");
Process process = Runtime.getRuntime().exec(shellstr);
if (process.waitFor() == 0) {
activityincome.finish();
}
} catch (IOException e) {
e.printStackTrace();
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "失败,可能设备不支持", Toast.LENGTH_SHORT).show();
activityincome.finish();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}