android 执行shell命令代码

 

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;
}
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值