android在apk中擷取root許可權,並執行命令

來源:互聯網
上載者:User

工程代碼:http://download.csdn.net/detail/victoryckl/4610617

    在apk中,有時候需要root許可權,例如通過apk更新系統庫等system的檔案等,避免升級韌體,或者在apk中需要直接存取某些裝置等。下面是在apk中擷取root許可權的方法,前提是裝置已經root過了。

   關鍵點在於下面這句,通過執行su產生一個具有root許可權的進程:

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

然後,在向這個進程的寫入要執行的命令,即可達到以root許可權執行命令:

dos = new DataOutputStream(p.getOutputStream());

dos.writeBytes(cmd + "\n");

dos.flush();

或者用下面的方式:

Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c", cmd});

經過測試,以root許可權執行命令,只在真機上測試成功,在模擬器上沒有成功過。

第一次運行時,會出現請求root許可權的介面,選中記住,並允許:

測試程式介面,如果已經root,介面中可以顯示出/system分區對應的裝置節點:

主要檔案:RootCmd.java

package org.ckl.root;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import android.util.Log;public final class RootCmd {private static final String TAG = "RootCmd";private static boolean mHaveRoot = false;// 判斷機器Android是否已經root,即是否擷取root許可權public static boolean haveRoot() {if (!mHaveRoot) {int ret = execRootCmdSilent("echo test"); // 通過執行測試命令來檢測if (ret != -1) {Log.i(TAG, "have root!");mHaveRoot = true;} else {Log.i(TAG, "not root!");}} else {Log.i(TAG, "mHaveRoot = true, have root!");}return mHaveRoot;}// 執行命令並且輸出結果public static String execRootCmd(String cmd) {String result = "";DataOutputStream dos = null;DataInputStream dis = null;try {Process p = Runtime.getRuntime().exec("su");// 經過Root處理的android系統即有su命令dos = new DataOutputStream(p.getOutputStream());dis = new DataInputStream(p.getInputStream());Log.i(TAG, cmd);dos.writeBytes(cmd + "\n");dos.flush();dos.writeBytes("exit\n");dos.flush();String line = null;while ((line = dis.readLine()) != null) {Log.d("result", line);result += line;}p.waitFor();} catch (Exception e) {e.printStackTrace();} finally {if (dos != null) {try {dos.close();} catch (IOException e) {e.printStackTrace();}}if (dis != null) {try {dis.close();} catch (IOException e) {e.printStackTrace();}}}return result;}// 執行命令但不關注結果輸出public static int execRootCmdSilent(String cmd) {int result = -1;DataOutputStream dos = null;try {Process p = Runtime.getRuntime().exec("su");dos = new DataOutputStream(p.getOutputStream());Log.i(TAG, cmd);dos.writeBytes(cmd + "\n");dos.flush();dos.writeBytes("exit\n");dos.flush();p.waitFor();result = p.exitValue();} catch (Exception e) {e.printStackTrace();} finally {if (dos != null) {try {dos.close();} catch (IOException e) {e.printStackTrace();}}}return result;}}

相關檔案:SystemPartition.java,擷取/system分區裝置節點,並支援重新mount /system為可讀寫:

package org.ckl.root;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import android.util.Log;public class SystemPartition {private static final String TAG = "SystemMount";private static String TMP_PATH = "/sdcard/mount.txt";private static String mMountPiont = null;private static boolean mWriteable = false;private SystemPartition() {Log.i(TAG, "new SystemMount()");}private static class SystemPartitionHolder {private static SystemPartition instance = new SystemPartition();}public SystemPartition getInstance() {return SystemPartitionHolder.instance;}public static String getSystemMountPiont() {DataInputStream dis = null;if (mMountPiont == null) { try {RootCmd.execRootCmd("mount > " + TMP_PATH);//Runtime.getRuntime().exec("mount > " + TMP_PATH);dis = new DataInputStream(new FileInputStream(TMP_PATH));String line = null;int index = -1;while ( (line = dis.readLine()) != null ) {index = line.indexOf(" /system ");if (index > 0) {mMountPiont = line.substring(0, index);if (line.indexOf(" rw") > 0) {mWriteable = true;Log.i(TAG, "/system is writeable !");} else {mWriteable = false;Log.i(TAG, "/system is readonly !");}break;}}} catch (Exception e) {e.printStackTrace();} finally {if (dis != null) {try {dis.close();} catch (IOException e1) {e1.printStackTrace();}dis = null;}File f = new File(TMP_PATH);if (f.exists()) {f.delete();}}}if (mMountPiont != null) {Log.i(TAG, "/system mount piont: " + mMountPiont);} else {Log.i(TAG, "get /system mount piont failed !!!");}return mMountPiont;}public static boolean isWriteable() {mMountPiont = null;getSystemMountPiont();return mWriteable;}public static void remountSystem(boolean writeable) {String cmd = null;getSystemMountPiont();if (mMountPiont != null && RootCmd.haveRoot()) {if (writeable) {cmd = "mount -o remount,rw " + mMountPiont + " /system";} else {cmd = "mount -o remount,ro " + mMountPiont + " /system";}RootCmd.execRootCmdSilent(cmd);isWriteable();}}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.