1,需要权限
<uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
2,下载 RootTools.jar包。
3,两个关键方法。主要是获取shell,并执行命令行。
方法如下:
private static boolean waitForCommand(Command cmd) { while (!cmd.isFinished()) { synchronized (cmd) { try { if (!cmd.isFinished()) { cmd.wait(2000); } } catch (InterruptedException e) { e.printStackTrace(); } } if (!cmd.isExecuting() && !cmd.isFinished()) { // Logger.errorST("Error: Command is not executing and is not finished!"); return false; } } //Logger.debug("Command Finished!"); return true; } public static ArrayList<String> runAndWait1(String cmd, final boolean root) { final ArrayList<String> output = new ArrayList<String>(); Command cc = new Command(1, cmd) { @Override public void commandOutput(int i, String s) { output.add(s); // System.out.println("output "+root+s); } @Override public void commandTerminated(int i, String s) { System.out.println("error" + root + s); } @Override public void commandCompleted(int i, int i2) { } }; try { RootTools.getShell(root).add(cc); } catch (Exception e) { // Logger.errorST("Exception when trying to run shell command", e); e.printStackTrace(); return null; } if (!waitForCommand(cc)) { return null; } return output; }
4,接下来就是简单的调用了。
final File f=new File("/data/misc/wifi/wpa_supplicant.conf"); new Thread(){ @Override public void run() { super.run(); ArrayList<String> list=new ArrayList<String>(); // String cpath = getCommandLineString(f.getPath()); String s="cat " + f.getPath(); list = runAndWait1(s, true); for (int i = 0; i < list.size(); i++) { Log.e("content",list.get(i)); } } }.start();
输出结果如下:
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-05 02:52:15