Android Java code calls Linux commands

Source: Internet
Author: User

To use a Linux Command, you must first obtain the root user. Remember to add busybox when executing the command. Generally, the basic method of Java I/O cannot place the file into a USB file. You can use Linux commands.

Processbuilder Pb = new processbuilder ("/system/bin/sh ");
// Java. Lang. processbuilder: Creates operating system processes.
PB. Directory (new file ("/"); // you can specify the current directory of the shell.
Try {
Process proc = Pb. Start ();
// Obtain the input stream, which can be used to obtain the shell output.
Bufferedreader in = new bufferedreader (New inputstreamreader (Proc. getinputstream ()));
Bufferedreader err = new bufferedreader (New inputstreamreader (Proc. geterrorstream ()));
// Obtain the output stream, which can be used to send commands to shell.
Printwriter out = new printwriter (New bufferedwriter (New outputstreamwriter (Proc
. Getoutputstream (), true );
Out. println ("PWD ");
Out. println ("su root"); // when this sentence is executed, a dialog box is displayed (the following program requires the highest permission to be granted...), asking the user to confirm.
Out. println ("CD/data/Data"); // This directory must have the root permission in the system for access.
Out. println ("ls-L"); // if this command can list the data file storage directory of the currently installed APK, it means that we have the root permission.
Out. println ("exit ");
// Proc. waitfor ();
String line;
While (line = in. Readline ())! = NULL ){
System. Out. println (line); // print the output result.
}
While (line = err. Readline ())! = NULL ){
System. Out. println (line); // print the error output
}
In. Close ();
Out. Close ();
Proc. Destroy ();
} Catch (exception e ){
System. Out. println ("exception:" + E );
}

 

 

 

 

 

 

 

File superuser = new file ("/system/bin/superuser ");

If (superuser. exists ())
{
// Return device to original state
Process = runtime.getruntime(cmd.exe C ("superuser ");
Dataoutputstream OS = new dataoutputstream (process. getoutputstream ());
OS. writebytes ("Mount-oremount, RW/dev/block/mtdblock3/System \ n ");
OS. writebytes ("busybox CP/system/bin/superuser/system/bin/su \ n ");
OS. writebytes ("busybox chown 0: 0/system/bin/su \ n ");
OS. writebytes ("chmod 4755/system/bin/su \ n ");
OS. writebytes ("RM/system/bin/superuser \ n ");
OS. writebytes ("Exit \ n ");
OS. Flush ();
}

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.