Android 开发中使用Linux Shell实例详解
引言
Android系统是基于Linux内核运行的,而做为一名Linux粉,不在Android上面运行一下Linux Shell怎么行呢?
最近发现了一个很好的Android Shell工具代码,在这里分享一下。
Shell核心代码
代码如下importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.List;
/**
* ShellUtils
*
* Check root
* {@link ShellUtils#checkRootPermission()}
*
*
* Execte command
* {@link ShellUtils#execCommand(String, boolean)}
* {@link ShellUtils#execCommand(String, boolean, boolean)}
* {@link ShellUtils#execCommand(List, boolean)}
* {@link ShellUtils#execCommand(List, boolean, boolean)}
* {@link ShellUtils#execCommand(String[], boolean)}
* {@link ShellUtils#execCommand(String[], boolean, boolean)}
*
*/
publicclassShellUtils {
publicstaticfinalString COMMAND_SU ="su";
publicstaticfinalString COMMAND_SH ="sh";
publicstaticfinalString COMMAND_EXIT ="exit\n";
publicstaticfinalString COMMAND_LINE_END ="\n";
privateShellUtils() {
thrownewAssertionError();
}
/**
* check whether has root permission
*
* @return
*/
publicstaticbooleancheckRootPermission() {
returnexecCommand("echo root",true,false).result ==0;
}
/**
* execute shell command, default return result msg
*
* @param command command
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResult execCommand(String command,booleanisRoot) {
returnexecCommand(newString[] {command}, isRoot,true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command list
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResult execCommand(List
returnexecCommand(commands ==null?null: commands.toArray(newString[] {}), isRoot,true);
}
/**
* execute shell commands, default return result msg
*
* @param commands command array
* @param isRoot whether need to run with root
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResult execCommand(String[] commands,booleanisRoot) {
returnexecCommand(commands, isRoot,true);
}
/**
* execute shell command
*
* @param command command
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResult execCommand(String command,booleanisRoot,booleanisNeedResultMsg) {
returnexecCommand(newString[] {command}, isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command list
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* @see ShellUtils#execCommand(String[], boolean, boolean)
*/
publicstaticCommandResult execCommand(List
returnexecCommand(commands ==null?null: commands.toArray(newString[] {}), isRoot, isNeedResultMsg);
}
/**
* execute shell commands
*
* @param commands command array
* @param isRoot whether need to run with root
* @param isNeedResultMsg whether need result msg
* @return
* if isNeedResultMsg is false, {@link CommandResult#successMsg} is null and
* {@link CommandResult#errorMsg} is null.
* if {@link CommandResult#result} is -1, there maybe some excepiton.
*
*/
publicstaticCommandResult execCommand(String[] commands,booleanisRoot,booleanisNeedResultMsg) {
intresult = -1;
if(commands ==null|| commands.length ==0) {
returnnewCommandResult(result,null,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 =newDataOutputStream(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();
// get command result
if(isNeedResultMsg) {
successMsg =newStringBuilder();
errorMsg =newStringBuilder();
successResult =newBufferedReader(newInputStreamReader(process.getInputStream()));
errorResult =newBufferedReader(newInputStreamReader(process.getErrorStream()));
String s;
while((s = successResult.readLine()) !=null) {
successMsg.append(s);
}
while((s = errorResult.readLine()) !=null) {
errorMsg.append(s);
}
}
}catch(IOException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}finally{
try{
if(os !=null) {
os.close();
}
if(successResult !=null) {
successResult.close();
}
if(errorResult !=null) {
errorResult.close();
}
}catch(IOException e) {
e.printStackTrace();
}
if(process !=null) {
process.destroy();
}
}
returnnewCommandResult(result, successMsg ==null?null: successMsg.toString(), errorMsg ==null?null
: errorMsg.toString());
}
/**
* result of command
*
* {@link CommandResult#result} means result of command, 0 means normal, else means error, same to excute in
* linux shell
* {@link CommandResult#successMsg} means success message of command result
* {@link CommandResult#errorMsg} means error message of command result
*
*/
publicstaticclassCommandResult {
/** result of command **/
publicint result;
/** success message of command result **/
publicString successMsg;
/** error message of command result **/
publicString errorMsg;
publicCommandResult(intresult) {
this.result = result;
}
publicCommandResult(intresult, String successMsg, String errorMsg) {
this.result = result;
this.successMsg = successMsg;
this.errorMsg = errorMsg;
}
}
}
ShellUtils代码引用自:Trinea
小实例
是否root
代码如下publicBoolean isRooted(){
CommandResult cmdResult = ShellUtils.execCommand("su",true);
if(cmdResult.errorMsg.equals("Permission denied") || cmdResult.result !=0) {
returnfalse;
}else{
returntrue;
}
}
复制文件
代码如下String[] commands =newString[] {"mount -o rw,remount /system","cp /mnt/sdcard/xx.apk /system/app/"};
publicbooleancopyFile(String[] cmdText){
CommandResult cmdResult = ShellUtils.execCommand(cmdText,true);
if(cmdResult.errorMsg.equals("Permission denied") || cmdResult.result !=0) {
returnfalse;
}else{
returntrue;
}
}
[android studio]Android 开发中使用Linux Shell实例详解
http://m.bbyears.com/caozuoxitong/133547.html
推荐访问:android学习路线