Android Java code execution adb Shell command
Android Java code execution adb Shell command
Use java code to replace the adb command
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
* @ 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 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
* @ 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 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
*
- If isNeedResultMsg is false, {@ link CommandResult # successMsg} is null and
* {@ Link CommandResult # errorMsg} is null.
*
- If {@ link CommandResult # result} is-1, there maybe some has iton.
*
*/
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(cmd.exe c (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
*
*
- {@ Link CommandResult # result} means result of command, 0 means normal, else means error, same to excute in
* Linux shell
*
- {@ Link CommandResult # successMsg} means success message of command result
*
- {@ Link CommandResult # errorMsg} means error message of command result
*
*
* @ Author Trinea 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
ExecCommand:
Java
Public CommandResult execCommand (String [] commands, boolean isRoot, boolean isNeedResultMsg)
Commands indicates the array of shell commands executed in sequence.
IsRoot indicates whether to run the command as su user (the mobile phone is already root)
IsNeedResultMsg indicates whether to store information about successful and failed command execution.
Java
/**
* Result of command
*
* @ Author Trinea 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 is the returned data structure. In the following example, result indicates the execution result. According to the linux Command Execution rule, 0 indicates that the execution is successful, and the other indicates the corresponding error code.
SuccessMsg stores the output information after successful execution, and errorMsg stores the output information after failed execution.
If isNeedResultMsg is false, successMsg and errorMsg are always empty, and result is still normal.
Other interfaces: Shell commands support String (single command), List (multiple commands), String [] (multiple commands)
See ShellUtils. java # Api Guide.
Use
Here are some examples of the current scenarios.
(1) copy an object
The principle is that adb executes the command:
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.exe cCommand (commands, true );
Note that the root account is not required to copy files. The root account is used to copy files to/system/app /.
(2) Silent Installation and uninstallation
Many of these friends have used the Android root permission to silently install or uninstall the application. The principle is to execute the command: pm install apkFilePath and pm uninstall packageName
Code details: PackageUtils installSilent (Context context, String filePath, String pmParams)
(3) modify the hosts file in Android
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 CommnandList = new ArrayList ();
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.exe cCommand (commnandList, true );
It is awesome to use the echo command to change the hosts file. It can take effect directly without restarting.