android n代码执行su,android上代码去执行"su"命令

执行“su”命令有一个前提,那就是手机被root过或者是手机里面的busybox支持“su”命令,否则会执行失败。

具体实现代码如下:

import java.io.BufferedReader;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import android.util.Log;

public abstract class AExecuteAsRoot {

public static boolean canRunRootCommands() {

boolean retval = false;

Process suProcess;

try {

suProcess = Runtime.getRuntime().exec("su");

DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

if (null != os && null != osRes) {

// Getting the id of the current user to check if this is root

os.writeBytes("id\n");

os.flush();

String currUid = osRes.readLine();

boolean exitSu = false;

if (null == currUid) {

retval = false;

exitSu = false;

Log.d("ROOT", "Can't get root access or denied by user");

} else if (true == currUid.contains("uid=0")) {

retval = true;

exitSu = true;

Log.d("ROOT", "Root access granted");

} else {

retval = false;

exitSu = true;

Log.d("ROOT", "Root access rejected: " + currUid);

}

if (exitSu) {

os.writeBytes("exit\n");

os.flush();

}

}

} catch (Exception e) {

// Can't get root !

// Probably broken pipe exception on trying to write to output

// stream after su failed, meaning that the device is not rooted

retval = false;

Log.d("ROOT",

"Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());

}

return retval;

}

public final boolean execute() {

boolean retval = false;

try {

ArrayListcommands = getCommandsToExecute();

if (null != commands && commands.size() > 0) {

Process process = Runtime.getRuntime().exec("su");

DataOutputStream os = new DataOutputStream(process.getOutputStream());

for (String currCommand : commands) {

os.writeBytes(currCommand + "\n");

os.flush();

}

os.writeBytes("exit\n");

os.flush();

BufferedReader reader = new BufferedReader(new InputStreamReader(

process.getInputStream()));

int read;

char[] buffer = new char[4096];

StringBuffer output = new StringBuffer();

while ((read = reader.read(buffer)) > 0) {

output.append(buffer, 0, read);

}

reader.close();

try {

int suProcessRetval = process.waitFor();

if (255 != suProcessRetval) {

retval = true;

} else {

retval = false;

}

System.out.println("BBBB: "+output.toString()) ;

} catch (Exception ex) {

//Log.e("Error executing root action", ex);

}

}

} catch (IOException ex) {

Log.w("ROOT", "Can't get root access", ex);

} catch (SecurityException ex) {

Log.w("ROOT", "Can't get root access", ex);

} catch (Exception ex) {

Log.w("ROOT", "Error executing internal operation", ex);

}

return retval;

}

protected abstract ArrayListgetCommandsToExecute();

}

import java.util.ArrayList;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

public class SuActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

try {

Log.d("ROOT", "result:" + new ExecuteAsRoot().execute());

} catch (Exception e) {

e.printStackTrace();

}

}

private class ExecuteAsRoot extends AExecuteAsRoot {

@Override

protected ArrayListgetCommandsToExecute() {

ArrayListlist = new ArrayList();

list.add("add kill-server");

list.add("adb devices");

return list;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值