android 执行shell命令方法
可执行su、sh命令 只需传入需要执行的命令即可进行执行
如:执行安装apk包命令:
new ShellCommand().sh.runWaitFor("pm install -r " + /.../apkName.apk)
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import android.util.Log;
public class ShellCommand {
private Boolean can_su;
public SH sh = new SH("sh");
public SH su = new SH("su");
public boolean canSU(){
return canSU(false);
}
public boolean canSU(boolean paramBoolean){
if ((this.can_su == null) || (paramBoolean)){
CommandResult localCommandResult = this.su.runWaitFor("id");
StringBuilder localStringBuilder = new StringBuilder();
if (localCommandResult.stdout != null){
localStringBuilder.append(localCommandResult.stdout).append(" ; ");
}
if (localCommandResult.stderr != null){
localStringBuilder.append(localCommandResult.stderr);
}
Log.v("ShellCommand.java", "canSU() su[" + localCommandResult.exit_value + "]: " + localStringBuilder);
this.can_su = Boolean.valueOf(localCommandResult.success());
}
return this.can_su.booleanValue();
}
public SH suOrSH(){
SH localSH;
if(!canSU()){
localSH = this.sh;
}else{
localSH = this.su;
}
return localSH;
}
public class CommandResult{
public final Integer exit_value;
public final String stderr;
public final String stdout;
public CommandResult(Integer paramString1, String paramString2, String arg4){
this.exit_value = paramString1;
this.stdout = paramString2;
this.stderr = arg4;
}
public boolean success(){
int i = 1;
if ((this.exit_value == null) || (this.exit_value.intValue() != 0)){
i = 0;
}
return i==1?true:false;
}
}
public class SH{
private String SHELL = "sh";
public SH(String arg2){
this.SHELL = arg2;
}
private String getStreamLines(InputStream paramInputStream){
String str = null;
StringBuffer localStringBuffer = null;
DataInputStream localDataInputStream = new DataInputStream(paramInputStream);
try{
if (localDataInputStream.available() > 0){
localStringBuffer = new StringBuffer(localDataInputStream.readLine());
}else{
localDataInputStream.close();
}
if (localStringBuffer != null){
str = localStringBuffer.toString();
return str;
}
}catch (Exception e){
Log.e("Exception", e.getMessage());
}
return str;
}
public Process run(String paramString){
Process localProcess = null;
try{
localProcess = Runtime.getRuntime().exec(this.SHELL);
DataOutputStream localDataOutputStream = new DataOutputStream(localProcess.getOutputStream());
localDataOutputStream.writeBytes("exec " + paramString + "\n");
localDataOutputStream.flush();
return localProcess;
}catch (Exception e){
Log.e("ShellCommand", "Exception on running: " + paramString + ":" + e.getMessage());
}
return localProcess;
}
public ShellCommand.CommandResult runWaitFor(String paramString){
Process localProcess = run(paramString);
Integer localInteger = null;
String str1 = null;
String str2 = null;
try{
localInteger = Integer.valueOf(localProcess.waitFor());
str1 = getStreamLines(localProcess.getInputStream());
str2 = getStreamLines(localProcess.getErrorStream());
return new ShellCommand.CommandResult(localInteger, str1, str2);
}catch (InterruptedException localInterruptedException){
Log.e("ShellCommand.java", "runWaitFor " + localInterruptedException.toString());
}catch (NullPointerException localNullPointerException){
Log.e("ShellCommand.java", "runWaitFor " + localNullPointerException.toString());
}
return null;
}
}
}