Implementation of running underlying Linux External commands on AndroidThe underlying layer of Android is actually the Linux kernel. Of course, you can run Linux commands in the shell environment, especially for Android systems that are processed by the root user. Basically, you can call Linux commands to completely control the mobile phone, the following rootcmd. java code is used to run Linux External commands.
Package my. android. code; import android. OS. environment; import Dalvik. annotation. signature; import Java. io. bufferedreader; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. file; import Java. io. filereader; import Java. io. inputstream; import Java. io. outputstream; import Java. util. vector; public final class rootcmd {// execute the Linux Command and output the result protected static vector execrootcmd (string par Amstring) {vector localvector = new vector (); try {process localprocess = runtime.getruntime(cmd.exe C ("Su"); // for Android systems processed by root, there is the su command outputstream localoutputstream = localprocess. getoutputstream (); dataoutputstream localdataoutputstream = new dataoutputstream (localoutputstream); inputstream localinputstream = localprocess. getinputstream (); datainputstream localdatainputstream = new datainputstr EAM (localinputstream); string str1 = string. valueof (paramstring); string str2 = str1 + "\ n"; localdataoutputstream. writebytes (str2); localdataoutputstream. flush (); string str3 = localdatainputstream. readline (); localvector. add (str3); localdataoutputstream. writebytes ("Exit \ n"); localdataoutputstream. flush (); localprocess. waitfor (); Return localvector;} catch (exception localexception) {localexcep Tion. printstacktrace () ;}}// run the Linux Command but do not care about the result output protected static int execrootcmdsilent (string paramstring) {try {process localprocess = runtime.getruntime(cmd.exe C ("Su "); object localobject = localprocess. getoutputstream (); dataoutputstream localdataoutputstream = new dataoutputstream (outputstream) localobject); string STR = string. valueof (paramstring); localobject = STR + "\ n"; localdataoutputstr EAM. writebytes (string) localobject); localdataoutputstream. flush (); localdataoutputstream. writebytes ("Exit \ n"); localdataoutputstream. flush (); localprocess. waitfor (); localobject = localprocess. exitvalue (); Return localobject;} catch (exception localexception) {localexception. printstacktrace () ;}}// determine whether the android server is root, that is, whether the root permission is obtained. Protected static Boolean haveroot () {int I = execrootcmdsilent ("Echo test"); // check if (I! =-1) return true; retrun false ;}}