Android应用申请ROOT权限

runtime = Runtime.getRuntime();

这句话在java中的效果为获取电脑终端,比如在window下面就是dos窗口, 如: runtime.exec("color 2D") 与直接在dos下面直接输入color 2D的效果一样。

在安卓中相当于获取到Linux终端。

 要让Android应用获得Root权限,首先Android设备必须已经获得Root权限。

应用获取Root权限的原理:让应用的代码执行目录获取最高权限。在Linux中通过chmod 777 [代码执行目录]

/**
* 应用程序运行命令获取 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 .writeChars(cmd+" ");
      os .writeChars("exit ");

      os.flush();
      process.waitFor();
    } catch (Exception e) {
      return false;
    } finally {
      try {
        if (os != null) {
          os.close();
        }
     process.destroy();
    } catch (Exception e) {
  }
  }
  return true;
}

其中pkgCodePath可通过getPackageCodePath()获得

判断应用是否获取root权限

  public synchronized boolean getRootAhth()
  {
    Process process = null;
    DataOutputStream os = null;
    try{
      process = Runtime.getRuntime().exec("su");
      os = new DataOutputStream(process.getOutputStream());
      os.writeBytes("exit ");
      os.flush();
      int exitValue = process.waitFor();
      if (exitValue == 0){
        return true;
      } else{
        return false;
      }
    } catch (Exception e){
      Log.d("*** DEBUG ***", "Unexpected error - Here is what I know: "+ e.getMessage());
      return false;
    } finally{
    try{
      if (os != null){
        os.close();
      }
      process.destroy();
  } catch (Exception e)
  {
  e.printStackTrace();
  }
  }
  }

不需要root权限,只需要声明 :<uses-permission android:name="android.permission.READ_LOGS"/>就可以拿到手机所有的日志信息

也就是在开发中logcat里面显示的内容,这部分内容由windowmanager 维护。

  1. 可以看到用户安装了什么应用。

     2. 可以看到自己应用的错误,便于后期软件维护的日志抓取。

原文地址:https://www.cnblogs.com/android-er/p/5478298.html