import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
public class ShellUtil {
/**
* check whether has root permission
*/
public static boolean hasRootPermission() {
return execCommand("echo root", true, false).result == 0;
}
public static CommandResult execCommand(String command, boolean isRoot) {
return execCommand(new String[] {command}, isRoot, true);
}
public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(new String[]{command}, isRoot, isNeedResultMsg);
}
public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, isNeedResultMsg);
}
/**
* execute shell commands
* {@link CommandResult#result} is -1, there maybe some excepiton.
*
* @param commands command array
* @param isRoot whether need to run with root
* @param needResponse whether need result msg
*/
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean needResponse) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, "空命令");
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
}
// donnot use os.writeBytes(commmand), avoid chinese charset error
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result = process.waitFor();
if (needResponse) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
successMsg.append(s).append("\n");
}
while ((s = errorResult.readLine()) != null) {
errorMsg.append(s);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (errorResult != null) {
errorResult.close();
}
if (successResult != null) {
successResult.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (process != null) {
process.destroy();
}
}
}
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
: errorMsg.toString());
}
public static class CommandResult {
public int result;
public String responseMsg;
public String errorMsg;
public CommandResult(int result) {
this.result = result;
}
public CommandResult(int result, String responseMsg, String errorMsg) {
this.result = result;
this.responseMsg = responseMsg;
this.errorMsg = errorMsg;
}
}
public static final String COMMAND_SU = "su";
public static final String COMMAND_SH = "sh";
public static final String COMMAND_EXIT = "exit\n";
public static final String COMMAND_LINE_END = "\n";
Android 代碼執(zhí)行命令
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
- 文/潘曉璐 我一進店門徒河,熙熙樓的掌柜王于貴愁眉苦臉地迎上來系馆,“玉大人,你說我怎么就攤上這事顽照∮赡ⅲ” “怎么了闽寡?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長尼酿。 經(jīng)常有香客問我爷狈,道長,這世上最難降的妖魔是什么裳擎? 我笑而不...
- 正文 為了忘掉前任涎永,我火速辦了婚禮,結(jié)果婚禮上鹿响,老公的妹妹穿的比我還像新娘土辩。我一直安慰自己,他們只是感情好抢野,可當我...
- 文/花漫 我一把揭開白布拷淘。 她就那樣靜靜地躺著,像睡著了一般指孤。 火紅的嫁衣襯著肌膚如雪启涯。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼鸣峭,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了酥艳?” 一聲冷哼從身側(cè)響起摊溶,我...
- 正文 年R本政府宣布,位于F島的核電站咨跌,受9級特大地震影響沪么,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜锌半,卻給世界環(huán)境...
- 文/蒙蒙 一禽车、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧刊殉,春花似錦殉摔、人聲如沸。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至遍膜,卻和暖如春碗硬,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瓢颅。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 添加主機列表,hosts文件的內(nèi)容: servers=(ali-cache ali-web1 ali-web2 a...
- 第一次運行*.java文件時角塑,遇到了 ‘javac不是’內(nèi)部或外部命令“錯誤:找不到或無法加載主類” 兩個問題 1...
- 時至午夜圃伶,才得空閑。 三歲的兒子最近暫住在爸媽家蒲列。今天媽媽發(fā)來一段小視頻窒朋,視頻中爸爸領著兒子在公園里走的背影。 公...
- 前期:雨后公園欺劳,小小黃他爹小黃唧取,發(fā)現(xiàn)垃圾桶上的煙灰缸有半缸水,用力一拍划提,濺起的水珠像小噴泉一樣枫弟,覺得挺好玩兒,于是...