Implementation of running underlying Linux External commands on Android

Source: Internet
Author: User
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 ;}}

 

Related Article

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.