android 应用请求root权限

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 05:57   550   0
应用程序有时候需要获取root权限的时候,需要向手机发送root请求,不过前提是手机必须root才有意义,那么就存在检测手机是否root,之后才可以请求root权限
下面是检测是否获取了root权限
 /**
  * 应用程序运行命令获取 Root权限,设备必须已破解(获得ROOT权限)
  * 
  * @return 应用程序是/否获取Root权限
  */
 public static boolean upgradeRootPermission(String pkgCodePath) {
     Process process = null;
     DataOutputStream os = null;
     try {
         String cmd="chmod 777 " + pkgCodePath;
         process = Runtime.getRuntime().exec("su"); //切换到root帐号
         os = new DataOutputStream(process.getOutputStream());
         os.writeBytes(cmd + "\n");
         os.writeBytes("exit\n");
         os.flush();
         process.waitFor();
     } catch (Exception e) {
         return false;
     } finally {
         try {
             if (os != null) {
                 os.close();
             }
             process.destroy();
         } catch (Exception e) {
         }
     }
     return true;
 }

请求root权限的方法:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //当前应用的代码执行目录
    upgradeRootPermission(getPackageCodePath());
}



分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP