Android: Requesting root access in your app

本文介绍了一种在Android应用中请求Root权限的方法,通过运行su命令来尝试获取超级用户权限,并进一步验证是否成功获得权限。该方法适用于已越狱或具有超级用户权限的设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转帖:http://www.stealthcopter.com/blog/2010/01/android-requesting-root-access-in-your-app/

 

 

This snippet shows how root access can be requested inside an application in order to write a file into a place we do not have permission to access usually. Requesting root access will only work if your phone allows it, or it has been ‘rooted’ (hacked to allow superuser permissions).

  1. Process p;   
  2. try {   
  3.    // Preform su to get root privledges   
  4.    p = Runtime.getRuntime().exec("su");    
  5.   
  6.    // Attempt to write a file to a root-only   
  7.    DataOutputStream os = new DataOutputStream(p.getOutputStream());   
  8.    os.writeBytes("echo /"Do I have root?/" >/system/sd/temporary.txt/n");   
  9.   
  10.    // Close the terminal   
  11.    os.writeBytes("exit/n");   
  12.    os.flush();   
  13.    try {   
  14.       p.waitFor();   
  15.            if (p.exitValue() != 255) {   
  16.               // TODO Code to run on success   
  17.               toastMessage("root");   
  18.            }   
  19.            else {   
  20.                // TODO Code to run on unsuccessful   
  21.                toastMessage("not root");   
  22.            }   
  23.    } catch (InterruptedException e) {   
  24.       // TODO Code to run in interrupted exception   
  25.        toastMessage("not root");   
  26.    }   
  27. catch (IOException e) {   
  28.    // TODO Code to run in input/output exception   
  29.     toastMessage("not root");   
  30. }  
Process p;
try {
   // Preform su to get root privledges
   p = Runtime.getRuntime().exec("su"); 

   // Attempt to write a file to a root-only
   DataOutputStream os = new DataOutputStream(p.getOutputStream());
   os.writeBytes("echo /"Do I have root?/" >/system/sd/temporary.txt/n");

   // Close the terminal
   os.writeBytes("exit/n");
   os.flush();
   try {
      p.waitFor();
           if (p.exitValue() != 255) {
        	  // TODO Code to run on success
              toastMessage("root");
           }
           else {
        	   // TODO Code to run on unsuccessful
        	   toastMessage("not root");
           }
   } catch (InterruptedException e) {
      // TODO Code to run in interrupted exception
	   toastMessage("not root");
   }
} catch (IOException e) {
   // TODO Code to run in input/output exception
	toastMessage("not root");
}

Where my “toastMessage” is just a function which creates a toast to display on the screen. On phones with superuser permissions installed (root access) this will display a dialog asking the user to allow or deny the application permission to have root access:

 

android request root access dialog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值