最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android 开发中使用Linux Shell实例详解
时间:2022-06-25 23:24:14 编辑:袖梨 来源:一聚教程网
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; } } |
相关文章
- 时空中的绘旅人天宇之间怎么玩 绘旅人天宇之间活动玩法介绍 12-25
- QQ2024年度报告怎么看 2024qq年度报告玩法介绍 12-25
- 归龙潮珠砂什么时候up 归龙潮红缘绮梦卡池介绍 12-25
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25