免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2879 | 回复: 0
打印 上一主题 下一主题

Android应用程序请求root访问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-03-28 19:26 |只看该作者 |倒序浏览
Android应用程序请求root访问




前提是手机已经破解获取root权限
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. 1.Process p;

  2. 2.try {

  3. 3.   // Preform su to get root privledges

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

  5. 5.

  6. 6.   // Attempt to write a file to a root-only

  7. 7.   DataOutputStream os = new DataOutputStream(p.getOutputStream());

  8. 8.   os.writeBytes("echo \"Do I have root?\" >/system/sd/temporary.txt\n");

  9. 9.

  10. 10.   // Close the terminal

  11. 11.   os.writeBytes("exit\n");

  12. 12.   os.flush();

  13. 13.   try {

  14. 14.      p.waitFor();

  15. 15.           if (p.exitValue() != 255) {

  16. 16.                  // TODO Code to run on success

  17. 17.              toastMessage("root");

  18. 18.           }

  19. 19.           else {

  20. 20.                   // TODO Code to run on unsuccessful

  21. 21.                   toastMessage("not root");

  22. 22.           }

  23. 23.   } catch (InterruptedException e) {

  24. 24.      // TODO Code to run in interrupted exception

  25. 25.           toastMessage("not root");

  26. 26.   }

  27. 27.} catch (IOException e) {

  28. 28.   // TODO Code to run in input/output exception

  29. 29.        toastMessage("not root");

  30. 30.}
复制代码
复制代码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:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP