Android开发中使用LinuxShell实例详解
引言
Android系统是基于Linux内核运行的,而做为一名Linux粉,不在Android上面运行一下LinuxShell怎么行呢?
最近发现了一个很好的AndroidShell工具代码,在这里分享一下。
Shell核心代码
importjava.io.BufferedReader;
importjava.io.DataOutputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.List;
/**
*ShellUtils
*
*Checkroot
*- {@linkShellUtils#checkRootPermission()}
*
*
*Exectecommand
*- {@linkShellUtils#execCommand(String,boolean)}
*- {@linkShellUtils#execCommand(String,boolean,boolean)}
*- {@linkShellUtils#execCommand(List,boolean)}
*- {@linkShellUtils#execCommand(List,boolean,boolean)}
*- {@linkShellUtils#execCommand(String[],boolean)}
*- {@linkShellUtils#execCommand(String[],boolean,boolean)}
*
*/
publicclassShellUtils{
publicstaticfinalStringCOMMAND_SU="su";
publicstaticfinalStringCOMMAND_SH="sh";
publicstaticfinalStringCOMMAND_EXIT="exit\n";
publicstaticfinalStringCOMMAND_LINE_END="\n";
privateShellUtils(){
thrownewAssertionError();
}
/**
*checkwhetherhasrootpermission
*
*@return
*/
publicstaticbooleancheckRootPermission(){
returnexecCommand("echoroot",true,false).result==0;
}
/**
*executeshellcommand,defaultreturnresultmsg
*
*@paramcommandcommand
*@paramisRootwhetherneedtorunwithroot
*@return
*@seeShellUtils#execCommand(String[],boolean,boolean)
*/
publicstaticCommandResultexecCommand(Stringcommand,booleanisRoot){
returnexecCommand(newString[]{command},isRoot,true);
}
/**
*executeshellcommands,defaultreturnresultmsg
*
*@paramcommandscommandlist
*@paramisRootwhetherneedtorunwithroot
*@return
*@seeShellUtils#execCommand(String[],boolean,boolean)
*/
publicstaticCommandResultexecCommand(Listcommands,booleanisRoot){
returnexecCommand(commands==null?null:commands.toArray(newString[]{}),isRoot,true);
}
/**
*executeshellcommands,defaultreturnresultmsg
*
*@paramcommandscommandarray
*@paramisRootwhetherneedtorunwithroot
*@return
*@seeShellUtils#execCommand(String[],boolean,boolean)
*/
publicstaticCommandResultexecCommand(String[]commands,booleanisRoot){
returnexecCommand(commands,isRoot,true);
}
/**
*executeshellcommand
*
*@paramcommandcommand
*@paramisRootwhetherneedtorunwithroot
*@paramisNeedResultMsgwhetherneedresultmsg
*@return
*@seeShellUtils#execCommand(String[],boolean,boolean)
*/
publicstaticCommandResultexecCommand(Stringcommand,booleanisRoot,booleanisNeedResultMsg){
returnexecCommand(newString[]{command},isRoot,isNeedResultMsg);
}
/**
*executeshellcommands
*
*@paramcommandscommandlist
*@paramisRootwhetherneedtorunwithroot
*@paramisNeedResultMsgwhetherneedresultmsg
*@return
*@seeShellUtils#execCommand(String[],boolean,boolean)
*/
publicstaticCommandResultexecCommand(Listcommands,booleanisRoot,booleanisNeedResultMsg){
returnexecCommand(commands==null?null:commands.toArray(newString[]{}),isRoot,isNeedResultMsg);
}
/**
*executeshellcommands
*
*@paramcommandscommandarray
*@paramisRootwhetherneedtorunwithroot
*@paramisNeedResultMsgwhetherneedresultmsg
*@return
*- ifisNeedResultMsgisfalse,{@linkCommandResult#successMsg}isnulland
*{@linkCommandResult#errorMsg}isnull.
*- if{@linkCommandResult#result}is-1,theremaybesomeexcepiton.
*
*/
publicstaticCommandResultexecCommand(String[]commands,booleanisRoot,booleanisNeedResultMsg){
intresult=-1;
if(commands==null||commands.length==0){
returnnewCommandResult(result,null,null);
}
Processprocess=null;
BufferedReadersuccessResult=null;
BufferedReadererrorResult=null;
StringBuildersuccessMsg=null;
StringBuildererrorMsg=null;
DataOutputStreamos=null;
try{
process=Runtime.getRuntime().exec(isRoot?COMMAND_SU:COMMAND_SH);
os=newDataOutputStream(process.getOutputStream());
for(Stringcommand:commands){
if(command==null){
continue;
}
//donnotuseos.writeBytes(commmand),avoidchinesecharseterror
os.write(command.getBytes());
os.writeBytes(COMMAND_LINE_END);
os.flush();
}
os.writeBytes(COMMAND_EXIT);
os.flush();
result=process.waitFor();
//getcommandresult
if(isNeedResultMsg){
successMsg=newStringBuilder();
errorMsg=newStringBuilder();
successResult=newBufferedReader(newInputStreamReader(process.getInputStream()));
errorResult=newBufferedReader(newInputStreamReader(process.getErrorStream()));
Strings;
while((s=successResult.readLine())!=null){
successMsg.append(s);
}
while((s=errorResult.readLine())!=null){
errorMsg.append(s);
}
}
}catch(IOExceptione){
e.printStackTrace();
}catch(Exceptione){
e.printStackTrace();
}finally{
try{
if(os!=null){
os.close();
}
if(successResult!=null){
successResult.close();
}
if(errorResult!=null){
errorResult.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
if(process!=null){
process.destroy();
}
}
returnnewCommandResult(result,successMsg==null?null:successMsg.toString(),errorMsg==null?null
:errorMsg.toString());
}
/**
*resultofcommand
*
*- {@linkCommandResult#result}meansresultofcommand,0meansnormal,elsemeanserror,sametoexcutein
*linuxshell
*- {@linkCommandResult#successMsg}meanssuccessmessageofcommandresult
*- {@linkCommandResult#errorMsg}meanserrormessageofcommandresult
*
*/
publicstaticclassCommandResult{
/**resultofcommand**/
publicintresult;
/**successmessageofcommandresult**/
publicStringsuccessMsg;
/**errormessageofcommandresult**/
publicStringerrorMsg;
publicCommandResult(intresult){
this.result=result;
}
publicCommandResult(intresult,StringsuccessMsg,StringerrorMsg){
this.result=result;
this.successMsg=successMsg;
this.errorMsg=errorMsg;
}
}
}
ShellUtils代码引用自:Trinea
小实例
是否root
publicBooleanisRooted(){
CommandResultcmdResult=ShellUtils.execCommand("su",true);
if(cmdResult.errorMsg.equals("Permissiondenied")||cmdResult.result!=0){
returnfalse;
}else{
returntrue;
}
}
复制文件
String[]commands=newString[]{"mount-orw,remount/system","cp/mnt/sdcard/xx.apk/system/app/"};
publicbooleancopyFile(String[]cmdText){
CommandResultcmdResult=ShellUtils.execCommand(cmdText,true);
if(cmdResult.errorMsg.equals("Permissiondenied")||cmdResult.result!=0){
returnfalse;
}else{
returntrue;
}
}
我暂时就举这两个例子,只要你会Shell,什么操作都是可以的。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!