转帖: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).

view plain copy to clipboard print ?
  1. Processp;
  2. try{
  3. //Preformsutogetrootprivledges
  4. p=Runtime.getRuntime().exec("su");
  5. //Attempttowriteafiletoaroot-only
  6. DataOutputStreamos=newDataOutputStream(p.getOutputStream());
  7. os.writeBytes("echo/"DoIhaveroot?/">/system/sd/temporary.txt/n");
  8. //Closetheterminal
  9. os.writeBytes("exit/n");
  10. os.flush();
  11. try{
  12. p.waitFor();
  13. if(p.exitValue()!=255){
  14. //TODOCodetorunonsuccess
  15. toastMessage("root");
  16. }
  17. else{
  18. //TODOCodetorunonunsuccessful
  19. toastMessage("notroot");
  20. }
  21. }catch(InterruptedExceptione){
  22. //TODOCodetorunininterruptedexception
  23. toastMessage("notroot");
  24. }
  25. }catch(IOExceptione){
  26. //TODOCodetorunininput/outputexception
  27. toastMessage("notroot");
  28. }

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

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android消息处理机制原理解析
  2. Android(安卓)studio Error:(30, 31) 错
  3. [置顶] Android(安卓)轻松实现后台搭建+A
  4. 利用SharedPreferences(持久化技术)实现
  5. 【android】关于ListView的一些体会1
  6. Android(安卓)Studio Intent向上下活动传
  7. Android(安卓)RxJava 实战系列:从磁盘 /
  8. android 模拟器修改默认路径问题
  9. Android中LogCat输出日志的自定义
  10. 原 美团外卖Android(安卓)Crash治理之路