AI手机网,短视频直播 硬改改机 一键新机 群控软件 刷机定制

 找回密码
 立即注册
搜索
查看: 1581|回复: 0

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

[复制链接]
发表于 2020-4-25 17:56:07 | 显示全部楼层 |阅读模式

在apk中,有时候需要root权限,例如通过apk更新系统库等system的文件等,避免升级固件,或者在apk中需要直接访问某些设备等。下面是在apk中获取root权限的方法,前提是设备已经root过了。

   关键点在于下面这句,通过执行su产生一个具有root权限的进程:

  1. Process p = Runtime.getRuntime().exec("su");
  2. 然后,在向这个进程的写入要执行的命令,即可达到以root权限执行命令:
  3. dos = new DataOutputStream(p.getOutputStream());
  4. dos.writeBytes(cmd + "\n");
  5. dos.flush();
  6. 或者用下面的方式:
  7. Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c", cmd});
复制代码

经过测试,以root权限执行命令,只在真机上测试成功,在模拟器上没有成功过。

第一次运行时,会出现请求root权限的界面,选中记住,并允许


  1. 主要文件:RootCmd.java
  2. [java]
  3. package org.ckl.root;

  4. import java.io.DataInputStream;
  5. import java.io.DataOutputStream;
  6. import java.io.IOException;

  7. import android.util.Log;

  8. public final class RootCmd {

  9.     private static final String TAG = "RootCmd";
  10.     private static boolean mHaveRoot = false;

  11.     // 判断机器Android是否已经root,即是否获取root权限
  12.     public static boolean haveRoot() {
  13.         if (!mHaveRoot) {
  14.             int ret = execRootCmdSilent("echo test"); // 通过执行测试命令来检测
  15.             if (ret != -1) {
  16.                 Log.i(TAG, "have root!");
  17.                 mHaveRoot = true;
  18.             } else {
  19.                 Log.i(TAG, "not root!");
  20.             }
  21.         } else {
  22.             Log.i(TAG, "mHaveRoot = true, have root!");
  23.         }
  24.         return mHaveRoot;
  25.     }

  26.     // 执行命令并且输出结果
  27.     public static String execRootCmd(String cmd) {
  28.         String result = "";
  29.         DataOutputStream dos = null;
  30.         DataInputStream dis = null;
  31.          
  32.         try {
  33.             Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令
  34.             dos = new DataOutputStream(p.getOutputStream());
  35.             dis = new DataInputStream(p.getInputStream());

  36.             Log.i(TAG, cmd);
  37.             dos.writeBytes(cmd + "\n");
  38.             dos.flush();
  39.             dos.writeBytes("exit\n");
  40.             dos.flush();
  41.             String line = null;
  42.             while ((line = dis.readLine()) != null) {
  43.                 Log.d("result", line);
  44.                 result += line;
  45.             }
  46.             p.waitFor();
  47.         } catch (Exception e) {
  48.             e.printStackTrace();
  49.         } finally {
  50.             if (dos != null) {
  51.                 try {
  52.                     dos.close();
  53.                 } catch (IOException e) {
  54.                     e.printStackTrace();
  55.                 }
  56.             }
  57.             if (dis != null) {
  58.                 try {
  59.                     dis.close();
  60.                 } catch (IOException e) {
  61.                     e.printStackTrace();
  62.                 }
  63.             }
  64.         }
  65.         return result;
  66.     }

  67.     // 执行命令但不关注结果输出
  68.     public static int execRootCmdSilent(String cmd) {
  69.         int result = -1;
  70.         DataOutputStream dos = null;
  71.          
  72.         try {
  73.             Process p = Runtime.getRuntime().exec("su");
  74.             dos = new DataOutputStream(p.getOutputStream());
  75.             
  76.             Log.i(TAG, cmd);
  77.             dos.writeBytes(cmd + "\n");
  78.             dos.flush();
  79.             dos.writeBytes("exit\n");
  80.             dos.flush();
  81.             p.waitFor();
  82.             result = p.exitValue();
  83.         } catch (Exception e) {
  84.             e.printStackTrace();
  85.         } finally {
  86.             if (dos != null) {
  87.                 try {
  88.                     dos.close();
  89.                 } catch (IOException e) {
  90.                     e.printStackTrace();
  91.                 }
  92.             }
  93.         }
  94.         return result;
  95.     }
  96. }

  97. 相关文件:SystemPartition.java,获取/system分区设备节点,并支持重新mount /system为可读写:
  98. [java]
  99. package org.ckl.root;

  100. import java.io.DataInputStream;
  101. import java.io.File;
  102. import java.io.FileInputStream;
  103. import java.io.IOException;

  104. import android.util.Log;

  105. public class SystemPartition {
  106.     private static final String TAG = "SystemMount";
  107.     private static String TMP_PATH = "/sdcard/mount.txt";
  108.     private static String mMountPiont = null;
  109.     private static boolean mWriteable = false;
  110.      
  111.     private SystemPartition() {
  112.         Log.i(TAG, "new SystemMount()");
  113.     }
  114.      
  115.     private static class SystemPartitionHolder {
  116.         private static SystemPartition instance = new SystemPartition();
  117.     }
  118.      
  119.     public SystemPartition getInstance() {
  120.         return SystemPartitionHolder.instance;
  121.     }
  122.      
  123.     public static String getSystemMountPiont() {
  124.         DataInputStream dis = null;
  125.         if (mMountPiont == null) {  
  126.             try {
  127.                 RootCmd.execRootCmd("mount > " + TMP_PATH);
  128. //              Runtime.getRuntime().exec("mount > " + TMP_PATH);
  129.                  
  130.                 dis = new DataInputStream(new FileInputStream(TMP_PATH));
  131.                  
  132.                 String line = null;
  133.                 int index = -1;
  134.                 while ( (line = dis.readLine()) != null ) {
  135.                     index = line.indexOf(" /system ");
  136.                     if (index > 0) {
  137.                         mMountPiont = line.substring(0, index);
  138.                         if (line.indexOf(" rw") > 0) {
  139.                             mWriteable = true;
  140.                             Log.i(TAG, "/system is writeable !");
  141.                         } else {
  142.                             mWriteable = false;
  143.                             Log.i(TAG, "/system is readonly !");
  144.                         }
  145.                         break;
  146.                     }
  147.                 }
  148.             } catch (Exception e) {
  149.                 e.printStackTrace();
  150.             } finally {
  151.                 if (dis != null) {
  152.                     try {
  153.                         dis.close();
  154.                     } catch (IOException e1) {
  155.                         e1.printStackTrace();
  156.                     }
  157.                     dis = null;
  158.                 }
  159.                  
  160.                 File f = new File(TMP_PATH);
  161.                 if (f.exists()) {
  162.                     f.delete();
  163.                 }
  164.             }
  165.         }
  166.          
  167.         if (mMountPiont != null) {
  168.             Log.i(TAG, "/system mount piont: " + mMountPiont);
  169.         } else {
  170.             Log.i(TAG, "get /system mount piont failed !!!");
  171.         }
  172.          
  173.         return mMountPiont;
  174.     }
  175.      
  176.     public static boolean isWriteable() {
  177.         mMountPiont = null;
  178.         getSystemMountPiont();
  179.         return mWriteable;
  180.     }
  181.      
  182.     public static void remountSystem(boolean writeable) {
  183.         String cmd = null;
  184.         getSystemMountPiont();
  185.         if (mMountPiont != null && RootCmd.haveRoot()) {
  186.             if (writeable) {
  187.                 cmd = "mount -o remount,rw " + mMountPiont + " /system";
  188.             } else {
  189.                 cmd = "mount -o remount,ro " + mMountPiont + " /system";
  190.             }
  191.             RootCmd.execRootCmdSilent(cmd);
  192.             
  193.             isWriteable();
  194.         }
  195.     }
  196. }
复制代码



您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

技术交流售后群

QQ|小黑屋|手机版|站点找错-建议|AI手机网 |Sitemap



GMT+8, 2024-5-17 12:57 , Processed in 0.155236 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表