private boolean isRoot(String command){Process process = null;  DataOutputStream os = null;  try {  /*Executes the specified program in a separate native process.  * The new process inherits the environment of the caller. * Calling this method is equivalent to calling exec(prog, null, null). *///如果设备获取了root权限,那么程序执行su命令时,就会提示用户进行授权process = Runtime.getRuntime().exec("su"); os = new DataOutputStream(process.getOutputStream());  os.writeBytes(command+"\n");  os.writeBytes("exit\n");  os.flush(); process.waitFor();   } catch (Exception e) {   //如果已经root,但是用户选择拒绝授权,e.getMessage() = write failed: EPIPE (Broken pipe) //如果没有root,,e.getMessage()= Error running exec(). Command: [su] Working Directory: null Environment: null Logger.d("Unexpected error - Here is what I know: "+e.getMessage());   return false;   }   finally {  try {   if (os != null) {  os.close();   }   process.destroy();  } catch (Exception e) {  // nothing  }  }  return true; }


   

更多相关文章

  1. android studio在终端执行gradlew命令,报无效的环境
  2. Android 应用程序运行shell命令
  3. android java 执行shell命令(笔记)
  4. Android固件img文件的解包, 修改和打包的命令行操作
  5. [APP] Android 开发笔记 001-环境搭建与命令行创建项目
  6. Android Studio第三十四期 - git企业级应用命令
  7. 调试android ethernet 的常用命令
  8. Android 技巧:命令行运行 sqlite3
  9. Android的用户界面

随机推荐

  1. 修改progressbar的样式
  2. Android(安卓)图片放大和缩小
  3. Android应用欢迎界面实现
  4. UltimateRecyclerView
  5. Android:AppWidget、桌面小部件
  6. Android网络连接保持session测试
  7. 关于NotificationManager.notify 异常解
  8. android @override 报错
  9. ScrollView源码分析
  10. Script to make ffmepg for android