In the case of Android applications, it is often necessary to execute shell scripts to quickly implement certain functions;
Executing shell scripts in an Android application saves you a lot of tedious code and avoids unnecessary errors;
For example: When copying a folder, you can execute the CP command in the shell command to achieve the purpose, and in the code to implement the Copy folder, not only need to write a lot of tedious code, but also easy to fall into the recursive loop error;
For example: To get the file system read and write permissions, just to execute a shell script in a sentence mount-o Rw,remount/can be easily done;
For example: Delete a file under a folder, or a class of files, or all the files, only need to execute a shell script in a sentence rm-f * (with * wildcard characters to match) can easily be done;
Another example: Silent installation, only need to execute a shell script in a PM install-r can achieve the purpose;
If these are implemented in code, not only the amount of code increases, but also easy to create a lot of bugs, thankless!
If you can execute the shell script in the Android application to achieve the goal, you can save a lot of code, avoid many easy to make mistakes, simple and efficient, why not?
Here is an example of a tool class that executes shell scripts in an Android app for your reference:
1 Packagecom.example.test; 2 3 ImportJava.io.BufferedReader; 4 ImportJava.io.DataOutputStream; 5 Importjava.io.IOException; 6 ImportJava.io.InputStreamReader; 7 8 ImportAndroid.util.Log; 9 Ten /** One * Execute Shell Script Tool class A * @authorMountain - * - */ the Public classcommandexecution { - - Public Static FinalString TAG = "Commandexecution"; - + Public Final StaticString command_su = "SU"; - Public Final StaticString command_sh = "SH"; + Public Final StaticString command_exit = "exit\n"; A Public Final StaticString command_line_end = "\ n"; at - /** - * Command Execution Results - * @authorMountain - * - */ in Public Static classCommandresult { - Public intresult =-1; to PublicString errormsg; + PublicString successmsg; - } the * /** $ * Execute command- singlePanax Notoginseng * @paramCommand - * @paramIsRoot the * @return + */ A Public StaticCommandresult execcommand (String command,BooleanisRoot) { thestring[] Commands ={command}; + returnexeccommand (commands, isRoot); - } $ $ /** - * Execute command-Multiple lines - * @paramcommands the * @paramIsRoot - * @return Wuyi */ the Public StaticCommandresult execcommand (string[] commands,BooleanisRoot) { -Commandresult Commandresult =NewCommandresult (); Wu if(Commands = =NULL|| Commands.length = = 0)returnCommandresult; -Process Process =NULL; AboutDataOutputStream OS =NULL; $BufferedReader Successresult =NULL; -BufferedReader Errorresult =NULL; -StringBuilder successmsg =NULL; -StringBuilder errormsg =NULL; A Try { +Process = Runtime.getruntime (). EXEC (isRoot?command_su:command_sh); theOS =NewDataOutputStream (Process.getoutputstream ()); - for(String command:commands) { $ if(Command! =NULL) { the Os.write (Command.getbytes ()); the os.writebytes (command_line_end); the Os.flush (); the } - } in os.writebytes (command_exit); the Os.flush (); theCommandresult.result =process.waitfor (); About //Get error message theSuccessmsg =NewStringBuilder (); theErrorMsg =NewStringBuilder (); theSuccessresult =NewBufferedReader (NewInputStreamReader (Process.getinputstream ())); +Errorresult =NewBufferedReader (NewInputStreamReader (Process.geterrorstream ())); - String S; the while((s = successresult.readline ())! =NULL) Successmsg.append (s); Bayi while((s = errorresult.readline ())! =NULL) Errormsg.append (s); theCommandresult.successmsg =successmsg.tostring (); theCommandresult.errormsg =errormsg.tostring (); -LOG.I (TAG, Commandresult.result + "|" +commandresult.successmsg -+ " | " +commandresult.errormsg); the}Catch(IOException e) { theString errmsg =E.getmessage (); the if(ErrMsg! =NULL) { the log.e (TAG, errmsg); -}Else { the E.printstacktrace (); the } the}Catch(Exception e) {94String errmsg =E.getmessage (); the if(ErrMsg! =NULL) { the log.e (TAG, errmsg); the}Else { 98 E.printstacktrace (); About } -}finally { 101 Try { 102 if(OS! =NULL) Os.close (); 103 if(Successresult! =NULL) Successresult.close (); 104 if(Errorresult! =NULL) Errorresult.close (); the}Catch(IOException e) {106String errmsg =E.getmessage (); 107 if(ErrMsg! =NULL) { 108 log.e (TAG, errmsg); 109}Else { the E.printstacktrace (); 111 } the } 113 if(Process! =NULL) Process.destroy (); the } the returnCommandresult; the } 117 118}
How to execute shell scripts in an Android program