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;
}
}
}

更多相关文章

  1. Android 获取屏幕高宽度,密度,通知栏高度,截图等常用方法
  2. Android下的Console命令
  3. Android退出程序的多种方法
  4. Android数据共享 sharedPreferences 的使用方法
  5. Android Studio使用过程中遇到的问题(一)-- "value 2"异常解决方法
  6. 简单总结RectF、Rect 和Matrix ,还有Paint的使用方法

随机推荐

  1. Android如何获取多媒体文件信息
  2. Anatomy of an Android(安卓)Application
  3. Android(安卓)ViewPager与WP Pivot的视图
  4. 新书内容连载(3):Android(安卓)SDK中常用命
  5. android线性布局参数详解
  6. 手机和平板之外——带你理解跨设备的Andr
  7. 厂商开始独立,Android你怎么办?
  8. 新书内容连载(3):Android(安卓)SDK中常用命
  9. Android实现游戏摇杆的源代码
  10. Android的交叉编译工具