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 * * *
* Execte command * * * * * * * */ publicclassShellUtils {
publicstaticfinalString COMMAND_SU ="su"; publicstaticfinalString COMMAND_SH ="sh"; publicstaticfinalString COMMAND_EXIT ="exitn"; 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
* * * */ 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 *
* * * * */ 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; } } |