android上代码去执行"su"命令
16lz
2021-01-25
执行“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 { ArrayList<String> commands = 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 ArrayList<String> getCommandsToExecute();}
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 ArrayList<String> getCommandsToExecute() { ArrayList<String> list = new ArrayList<String>(); list.add("add kill-server"); list.add("adb devices"); return list; } }}
更多相关文章
- GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
- 如何在后台运行Linux命令?
- No.11 使用firewall配置的防火墙策略的生效模式
- 【android之ADB命令】ADB SHELL 命令乱码解决方法
- android自动关机代码
- Android开发常用命令行
- 如何单独编译Android源代码中的模块
- [android]android自动化测试十之单元测试实例
- android 杂记-- adb 使用笔记