android在apk中获取root权限,并执行命令

android在apk中获取root权限,并执行命令

标签: androidstringcmdexceptiondosnull
5917人阅读 评论(1) 收藏 举报
分类:

工程代码: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

  1. package org.ckl.root;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import android.util.Log;  
  8.   
  9. public final class RootCmd {  
  10.   
  11.     private static final String TAG = "RootCmd";  
  12.     private static boolean mHaveRoot = false;  
  13.   
  14.     // 判断机器Android是否已经root,即是否获取root权限  
  15.     public static boolean haveRoot() {  
  16.         if (!mHaveRoot) {  
  17.             int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测  
  18.             if (ret != -1) {  
  19.                 Log.i(TAG, "have root!");  
  20.                 mHaveRoot = true;  
  21.             } else {  
  22.                 Log.i(TAG, "not root!");  
  23.             }  
  24.         } else {  
  25.             Log.i(TAG, "mHaveRoot = true, have root!");  
  26.         }  
  27.         return mHaveRoot;  
  28.     }  
  29.   
  30.     // 执行命令并且输出结果  
  31.     public static String execRootCmd(String cmd) {  
  32.         String result = "";  
  33.         DataOutputStream dos = null;  
  34.         DataInputStream dis = null;  
  35.           
  36.         try {  
  37.             Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令  
  38.             dos = new DataOutputStream(p.getOutputStream());  
  39.             dis = new DataInputStream(p.getInputStream());  
  40.   
  41.             Log.i(TAG, cmd);  
  42.             dos.writeBytes(cmd + "\n");  
  43.             dos.flush();  
  44.             dos.writeBytes("exit\n");  
  45.             dos.flush();  
  46.             String line = null;  
  47.             while ((line = dis.readLine()) != null) {  
  48.                 Log.d("result", line);  
  49.                 result += line;  
  50.             }  
  51.             p.waitFor();  
  52.         } catch (Exception e) {  
  53.             e.printStackTrace();  
  54.         } finally {  
  55.             if (dos != null) {  
  56.                 try {  
  57.                     dos.close();  
  58.                 } catch (IOException e) {  
  59.                     e.printStackTrace();  
  60.                 }  
  61.             }  
  62.             if (dis != null) {  
  63.                 try {  
  64.                     dis.close();  
  65.                 } catch (IOException e) {  
  66.                     e.printStackTrace();  
  67.                 }  
  68.             }  
  69.         }  
  70.         return result;  
  71.     }  
  72.   
  73.     // 执行命令但不关注结果输出  
  74.     public static int execRootCmdSilent(String cmd) {  
  75.         int result = -1;  
  76.         DataOutputStream dos = null;  
  77.           
  78.         try {  
  79.             Process p = Runtime.getRuntime().exec("su");  
  80.             dos = new DataOutputStream(p.getOutputStream());  
  81.               
  82.             Log.i(TAG, cmd);  
  83.             dos.writeBytes(cmd + "\n");  
  84.             dos.flush();  
  85.             dos.writeBytes("exit\n");  
  86.             dos.flush();  
  87.             p.waitFor();  
  88.             result = p.exitValue();  
  89.         } catch (Exception e) {  
  90.             e.printStackTrace();  
  91.         } finally {  
  92.             if (dos != null) {  
  93.                 try {  
  94.                     dos.close();  
  95.                 } catch (IOException e) {  
  96.                     e.printStackTrace();  
  97.                 }  
  98.             }  
  99.         }  
  100.         return result;  
  101.     }  
  102. }  

相关文件:SystemPartition.java,获取/system分区设备节点,并支持重新mount /system为可读写:
  1. package org.ckl.root;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.IOException;  
  7.   
  8. import android.util.Log;  
  9.   
  10. public class SystemPartition {  
  11.     private static final String TAG = "SystemMount";  
  12.     private static String TMP_PATH = "/sdcard/mount.txt";  
  13.     private static String mMountPiont = null;  
  14.     private static boolean mWriteable = false;  
  15.       
  16.     private SystemPartition() {  
  17.         Log.i(TAG, "new SystemMount()");  
  18.     }  
  19.       
  20.     private static class SystemPartitionHolder {  
  21.         private static SystemPartition instance = new SystemPartition();  
  22.     }  
  23.       
  24.     public SystemPartition getInstance() {  
  25.         return SystemPartitionHolder.instance;  
  26.     }  
  27.       
  28.     public static String getSystemMountPiont() {  
  29.         DataInputStream dis = null;  
  30.         if (mMountPiont == null) {   
  31.             try {  
  32.                 RootCmd.execRootCmd("mount > " + TMP_PATH);  
  33. //              Runtime.getRuntime().exec("mount > " + TMP_PATH);  
  34.                   
  35.                 dis = new DataInputStream(new FileInputStream(TMP_PATH));  
  36.                   
  37.                 String line = null;  
  38.                 int index = -1;  
  39.                 while ( (line = dis.readLine()) != null ) {  
  40.                     index = line.indexOf(" /system ");  
  41.                     if (index > 0) {  
  42.                         mMountPiont = line.substring(0, index);  
  43.                         if (line.indexOf(" rw") > 0) {  
  44.                             mWriteable = true;  
  45.                             Log.i(TAG, "/system is writeable !");  
  46.                         } else {  
  47.                             mWriteable = false;  
  48.                             Log.i(TAG, "/system is readonly !");  
  49.                         }  
  50.                         break;  
  51.                     }  
  52.                 }  
  53.             } catch (Exception e) {  
  54.                 e.printStackTrace();  
  55.             } finally {  
  56.                 if (dis != null) {  
  57.                     try {  
  58.                         dis.close();  
  59.                     } catch (IOException e1) {  
  60.                         e1.printStackTrace();  
  61.                     }  
  62.                     dis = null;  
  63.                 }  
  64.                   
  65.                 File f = new File(TMP_PATH);  
  66.                 if (f.exists()) {  
  67.                     f.delete();  
  68.                 }  
  69.             }  
  70.         }  
  71.           
  72.         if (mMountPiont != null) {  
  73.             Log.i(TAG, "/system mount piont: " + mMountPiont);  
  74.         } else {  
  75.             Log.i(TAG, "get /system mount piont failed !!!");  
  76.         }  
  77.           
  78.         return mMountPiont;  
  79.     }  
  80.       
  81.     public static boolean isWriteable() {  
  82.         mMountPiont = null;  
  83.         getSystemMountPiont();  
  84.         return mWriteable;  
  85.     }  
  86.       
  87.     public static void remountSystem(boolean writeable) {  
  88.         String cmd = null;  
  89.         getSystemMountPiont();  
  90.         if (mMountPiont != null && RootCmd.haveRoot()) {  
  91.             if (writeable) {  
  92.                 cmd = "mount -o remount,rw " + mMountPiont + " /system";  
  93.             } else {  
  94.                 cmd = "mount -o remount,ro " + mMountPiont + " /system";  
  95.             }  
  96.             RootCmd.execRootCmdSilent(cmd);  
  97.               
  98.             isWriteable();  
  99.         }  
  100.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值