Android Java code execution adb shell command

Source: Internet
Author: User
Tags echo command

Android Java code execution adb shell command

Replace ADB commands with Java code

Add Tool class

Shellutils.java
Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.List;


/**
* Shellutils
*/
public class Shellutils {


public static final String Command_su = "SU";
public static final String command_sh = "SH";
public static final String command_exit = "exit\n";
public static final String command_line_end = "\ n";


Private Shellutils () {
throw new Assertionerror ();
}


/**
* Check whether has root permission
*
* @return
*/
public static Boolean checkrootpermission () {
Return ExecCommand ("Echo Root", true, false). result = = 0;
}


/**
* Execute shell command, default return result msg
*
* @param command command
* @param isRoot whether need to run with root
* @return
* @see Shellutils#execcommand (string[], Boolean, Boolean)
*/
public static Commandresult execcommand (String command, Boolean isRoot) {
Return ExecCommand (new string[] {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 (string[], Boolean, Boolean)
*/
public static Commandresult execcommand (list<string> commands, Boolean isRoot) {
return execcommand (commands = = null? Null:commands.toArray (new string[] {}), IsRoot, true);
}


/**
* Execute shell commands, default return result msg
*
* @param commands Command array
* @param isRoot whether need to run with root
* @return
* @see Shellutils#execcommand (string[], Boolean, Boolean)
*/
public static Commandresult ExecCommand (string[] commands, Boolean isRoot) {
return execcommand (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 static Commandresult execcommand (String command, Boolean isRoot, Boolean isneedresultmsg) {
Return ExecCommand (new string[] {command}, IsRoot, isneedresultmsg);
}


/**
* Execute shell commands
*
* @param commands Command list
* @param isRoot whether need to run with root
* @param isneedresultmsg whether need result msg
* @return
* @see Shellutils#execcommand (string[], Boolean, Boolean)
*/
public static Commandresult execcommand (list<string> commands, Boolean isRoot, Boolean isneedresultmsg) {
return execcommand (commands = = null? Null:commands.toArray (new string[] {}), isRoot, isneedresultmsg);
}


/**
* Execute shell commands
*
* @param commands Command array
* @param isRoot whether 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 commandresult#errormsg} is null.</li>
* <li>if {@link Commandresult#result} is-1, there maybe some excepiton.</li>
* </ul>
*/
public static Commandresult ExecCommand (string[] commands, Boolean isRoot, Boolean isneedresultmsg) {
int result =-1;
if (commands = = NULL | | commands.length = = 0) {
return new Commandresult (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 = new DataOutputStream (Process.getoutputstream ());
for (String command:commands) {
if (command = = null) {
Continue
}


Donnot use Os.writebytes (commmand), avoid Chinese charset error
Os.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 = new StringBuilder ();
ErrorMsg = new StringBuilder ();
Successresult = new BufferedReader (New InputStreamReader (Process.getinputstream ()));
Errorresult = new BufferedReader (New InputStreamReader (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 new Commandresult (result, successmsg = = null? null:successMsg.toString (), errormsg = = null? null
: Errormsg.tostring ());
}


/**
* Result of command
* <ul>
* <li>{@link Commandresult#result} means result of command, 0 means normal, else means error, same to Excute in
* Linux shell</li>
* <li>{@link commandresult#successmsg} means success message of command result</li>
* <li>{@link commandresult#errormsg} means error message of command result</li>
* </ul>
*
* @author <a href= "http://www.trinea.cn" target= "_blank" >Trinea</a> 2013-5-16
*/
public static class Commandresult {


/** Result of command **/
public int result;
/** success Message of command result **/
Public String successmsg;
/** error message of command result **/
Public String errormsg;


public commandresult (int result) {
This.result = result;
}


public commandresult (int result, string successmsg, String errormsg) {
This.result = result;
This.successmsg = successmsg;
This.errormsg = errormsg;
}
}
}








1, API Introduction
The following is the method execcommand for the final execution of the command in Shellutils:

Java


Public Commandresult execcommand (string[] commands, Boolean isRoot, Boolean isneedresultmsg)


Where commands represents an array of shell commands executed sequentially
IsRoot indicates whether to execute with SU User (requires phone already root)
Isneedresultmsg indicates whether to store information after the command execution succeeds and fails.
Java


/**
* Result of command
*
* @author <a href= "http://www.trinea.cn" target= "_blank" >Trinea</a> 2013-5-16
*/
public static class Commandresult {

/** Result of command **/
public int result;
/** success Message of command result **/
Public String successmsg;
/** error message of command result **/
Public String errormsg;

public commandresult (int result) {
This.result = result;
}

public commandresult (int result, string successmsg, String errormsg) {
This.result = result;
This.successmsg = successmsg;
This.errormsg = errormsg;
}
}
Commandresult for the returned data structure, where result represents the result of execution, according to the Linux Command execution rule, 0 indicates success, and the other is the corresponding error code.
Successmsg the output information after the successful execution of the store, errormsg the output information after the failed execution of the store.
If Isneedresultmsg is false,successmsg and errormsg will always be empty, and result is still the normal result.

Other interfaces, shell commands support execution of String (Single command), List (multiple commands), string[] (multiple commands)
See: Shellutils.java#api Guide

Use

Take the example of a few of your current scenarios

(1) Copy files
The principle is that ADB executes commands:
Mount-o Rw,remount/system
cp/mnt/sdcard/xx.apk/system/app/
The Java code is as follows:

string[] commands = new string[] {"Mount-o rw,remount/system", "cp/mnt/sdcard/xx.apk/system/app/"};
Commandresult result = Shellutils.execcommand (commands, true);
Note that the generic copy file does not require root, which is rooted because it needs to be copied to the/system/app/.
(2) silent installation and uninstallation
This many friends have already used Android root privileges to silently install or uninstall the app, the principle is to execute the command: PM Install apkfilepath and PM uninstall PackageName
Specific code visible: Packageutils installsilent (Context context, string FilePath, String pmparams)

(3) Android Modify Hosts file
The principle is to execute the command:
Mount-o Rw,remount/system
echo "127.0.0.1 localhost" >/etc/hosts
echo "185.31.17.184 github.global.ssl.fastly.net" >>/etc/hosts
chmod 644/etc/hosts
The code is as follows:
Java


list<string> commnandlist = new arraylist<string> ();
Commnandlist.add ("Mount-o rw,remount/system");
Commnandlist.add ("Echo \" 127.0.0.1 localhost\ ">/etc/hosts");
Commnandlist.add ("Echo \" 185.31.17.184 github.global.ssl.fastly.net\ ">>/etc/hosts");
Commnandlist.add ("chmod 644/etc/hosts");
Commandresult result = Shellutils.execcommand (Commnandlist, true);
Use the echo command to change the hosts file is very good oh, do not restart can be directly effective OH.



Android Java code execution adb shell command

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.