Android Code Execution Linux Shell little memory

Source: Internet
Author: User

Introduction

Android is based on the Linux kernel, and as a Linux powder, do not run on Android Linux shell on the line?
Recently found a good Android shell tool code, here to share a bit.

Shell Core Code
ImportJava.io.BufferedReader;ImportJava.io.DataOutputStream;ImportJava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.util.List;/** * shellutils * <ul> * <strong>check root</strong> * <li>{@link shellutils# Checkrootpermission ()}</li> * </ul> * <ul> * <strong>execte command</strong> * <li> {@link Shellutils#execcommand (String, Boolean)}</li> * <li>{@link Shellutils#execcommand (String, Boolean , Boolean)}</li> * <li>{@link Shellutils#execcommand (List, Boolean)}</li> * <li>{@link Shellutils#execcommand (List, Boolean, Boolean)}</li> * <li>{@link Shellutils#execcommand (string[], Boolean)}</li> * <li>{@link Shellutils#execcommand (string[], Boolean, Boolean)}</li> * </ul> * / Public  class shellutils {     Public Static FinalString Command_su ="Su"; Public Static FinalString Command_sh ="sh"; Public Static FinalString Command_exit ="exit\n"; Public Static FinalString Command_line_end ="\ n";Private shellutils() {Throw NewAssertionerror (); }/** * Check whether has root permission * * @return  */     Public Static Boolean checkrootpermission() {returnExecCommand ("Echo Root",true,false). result = =0; }/** * Execute shell command, default return result msg * * @param Command command * @par AM isRoot whether need to run with root * @return * @see shellutils#execcommand (string[], b Oolean, Boolean) * /     Public StaticCommandresultexeccommand(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 (stri Ng[], Boolean, Boolean) *
      /     Public StaticCommandresultexeccommand(list<string> commands,BooleanIsRoot) {returnExecCommand (Commands = =NULL?NULL: Commands.toarray (NewString[] {}), IsRoot,true); }/** * Execute shell commands, default return result msg * * @param commands command Array *< c6> @param IsRoot whether need to run with root * @return * @see Shellutils#execcommand (Str Ing[], Boolean, Boolean) * /     Public StaticCommandresultexeccommand(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) */     Public StaticCommandresultexeccommand(String command,BooleanIsRoot,BooleanISNEEDRESULTMSG) {returnExecCommand (Newstring[] {command}, IsRoot, isneedresultmsg); }/** * Execute shell commands * * @param commands Command list * @param isRoot wheth Er need to run with root * @param isneedresultmsg Whether need result msg * @return * @s EE shellutils#execcommand (string[], Boolean, Boolean) */     Public StaticCommandresultexeccommand(list<string> commands,BooleanIsRoot,BooleanISNEEDRESULTMSG) {returnExecCommand (Commands = =NULL?NULL: Commands.toarray (NewString[] {}), isRoot, isneedresultmsg); }/** * Execute shell commands * * @param commands Command Array * @param isRoot Whet     She need to run with root * @param isneedresultmsg Whether need result msg * @return <ul> * <li>if Isneedresultmsg is false, {@link commandresult#successmsg} is null and * {@link Com MANDRESULT#ERRORMSG} is null.</li> * <li>if {@link Commandresult#result} is-1, there maybe some e xcepiton.</li> * </ul> * *     Public StaticCommandresultexeccommand(string[] commands,BooleanIsRoot,BooleanISNEEDRESULTMSG) {intresult =-1;if(Commands = =NULL|| Commands.length = =0) {return NewCommandresult (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 errorOs.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 (); }        }return NewCommandresult (result, successmsg = =NULL?NULL: Successmsg.tostring (), errormsg = =NULL?NULL: Errormsg.tostring ()); }/** * Result of command * <ul> * <li>{@link Commandresult#result} means result of command, 0 m EANs Normal, else means error, same to Excute in * Linux shell</li> * <li>{@link commandresult#success  MSG} means success message of command result</li> * <li>{@link commandresult#errormsg} means error message of command result</li> * </ul> * *     Public Static  class commandresult {        /** result of command **/         Public intResult/** Success Message of command result **/         PublicString successmsg;/** error message of command result **/         PublicString errormsg; Public Commandresult(intResult) { This. result = result; } Public Commandresult(intresult, string successmsg, String errormsg) { This. result = result; This. successmsg = successmsg; This. errormsg = errormsg; }    }}

Shellutils code reference from: Trinea

Small instances

is root

publicisRooted(){    CommandResult cmdResult = ShellUtils.execCommand("su"true);    if (cmdResult.errorMsg.equals("Permission denied"0) {        returnfalse;    }else{        returntrue;    }}

Copying files

new"mount -o rw,remount /system""cp /mnt/sdcard/xx.apk /system/app/" };publicbooleancopyFile(String[] cmdText){    true);    if (cmdResult.errorMsg.equals("Permission denied"0) {        returnfalse;    }else{        returntrue;    }}

I'll give you these two examples for the time being, so long as you can shell, what you do is all right.

Blog name: Wang Leping Blog

Blog Address: http://blog.lepingde.com

CSDN Blog Address: http://blog.csdn.net/lecepin



Android Code Execution Linux Shell little memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.