Java >

Java >,java,android,root,Java,Android,Root,您所需要做的就是扩展这个类并重写getCommandsToExecute方法,以返回要作为root用户执行的命令 public abstract class ExecuteAsRootBase { public static boolean canRunRootCommands() { boolean retval = false; Process suProcess; try { suProcess = Runti

您所需要做的就是扩展这个类并重写
getCommandsToExecute
方法,以返回要作为root用户执行的命令

public abstract class ExecuteAsRootBase
{
   public static boolean canRunRootCommands()
   {
      boolean retval = false;
      Process suProcess;

      try
      {
         suProcess = Runtime.getRuntime().exec("su");

         DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
         DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

         if (null != os && null != osRes)
         {
            // Getting the id of the current user to check if this is root
            os.writeBytes("id\n");
            os.flush();

            String currUid = osRes.readLine();
            boolean exitSu = false;
            if (null == currUid)
            {
               retval = false;
               exitSu = false;
               Log.d("ROOT", "Can't get root access or denied by user");
            }
            else if (true == currUid.contains("uid=0"))
            {
               retval = true;
               exitSu = true;
               Log.d("ROOT", "Root access granted");
            }
            else
            {
               retval = false;
               exitSu = true;
               Log.d("ROOT", "Root access rejected: " + currUid);
            }

            if (exitSu)
            {
               os.writeBytes("exit\n");
               os.flush();
            }
         }
      }
      catch (Exception e)
      {
         // Can't get root !
         // Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted

         retval = false;
         Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
      }

      return retval;
   }

   public final boolean execute()
   {
      boolean retval = false;

      try
      {
         ArrayList<String> commands = getCommandsToExecute();
         if (null != commands && commands.size() > 0)
         {
            Process suProcess = Runtime.getRuntime().exec("su");

            DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

            // Execute commands that require root access
            for (String currCommand : commands)
            {
               os.writeBytes(currCommand + "\n");
               os.flush();
            }

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

            try
            {
               int suProcessRetval = suProcess.waitFor();
               if (255 != suProcessRetval)
               {
                  // Root access granted
                  retval = true;
               }
               else
               {
                  // Root access denied
                  retval = false;
               }
            }
            catch (Exception ex)
            {
               Log.e("ROOT", "Error executing root action", ex);
            }
         }
      }
      catch (IOException ex)
      {
         Log.w("ROOT", "Can't get root access", ex);
      }
      catch (SecurityException ex)
      {
         Log.w("ROOT", "Can't get root access", ex);
      }
      catch (Exception ex)
      {
         Log.w("ROOT", "Error executing internal operation", ex);
      }

      return retval;
   }
   protected abstract ArrayList<String> getCommandsToExecute();
}
公共抽象类ExecuteAsRootBase
{
公共静态布尔值canRunRootCommands()
{
布尔retval=false;
过程超过程;
尝试
{
supprocess=Runtime.getRuntime().exec(“su”);
DataOutputStream os=新的DataOutputStream(supprocess.getOutputStream());
DataInputStream osRes=新的DataInputStream(supprocess.getInputStream());
if(null!=os&&null!=osRes)
{
//正在获取当前用户的id以检查此用户是否为root用户
os.writeBytes(“id\n”);
os.flush();
字符串currUid=osRes.readLine();
布尔exitSu=false;
if(null==currUid)
{
retval=false;
exitSu=假;
Log.d(“ROOT”,“无法获得ROOT访问权限或被用户拒绝”);
}
else if(true==currUid.contains(“uid=0”))
{
retval=true;
exitSu=真;
Log.d(“根”,“授予根访问权”);
}
其他的
{
retval=false;
exitSu=真;
Log.d(“根”,“根访问被拒绝:”+currUid);
}
进出口银行
{
os.writeBytes(“退出”);
os.flush();
}
}
}
捕获(例外e)
{
//不能得到根!
//su失败后,尝试写入输出流(os)时可能出现管道中断异常,这意味着设备没有根目录
retval=false;
Log.d(“根”,“根访问被拒绝[”+e.getClass().getName()+”]:“+e.getMessage());
}
返回返回;
}
公共最终布尔值执行()
{
布尔retval=false;
尝试
{
ArrayList commands=getCommandsToExecute();
if(null!=commands&&commands.size()>0)
{
Process supprocess=Runtime.getRuntime().exec(“su”);
DataOutputStream os=新的DataOutputStream(supprocess.getOutputStream());
//执行需要根访问权限的命令
用于(字符串命令:命令)
{
os.writeBytes(currCommand+“\n”);
os.flush();
}
os.writeBytes(“退出”);
os.flush();
尝试
{
int supprocessretval=supprocess.waitFor();
if(255!=supprocessretval)
{
//已授予根访问权限
retval=true;
}
其他的
{
//根访问被拒绝
retval=false;
}
}
捕获(例外情况除外)
{
Log.e(“根”,“执行根操作时出错”,ex);
}
}
}
捕获(IOEX异常)
{
Log.w(“根”,“无法获得根访问权”,ex);
}
catch(SecurityException-ex)
{
Log.w(“根”,“无法获得根访问权”,ex);
}
捕获(例外情况除外)
{
Log.w(“根”,“执行内部操作时出错”,ex);
}
返回返回;
}
受保护的抽象ArrayList getCommandsToExecute();
}

实际上并没有任何方法来提升现有进程的权限,只有启动以root用户身份运行的新进程的方法。例如,向su传递一个要执行的命令,请参见@Chris如何启动一个新的Activity/Task/Android java app/etc?这将需要对Android进行巨大的更改。通常情况下,您不会为android java执行创建进程,而是让android请求“zygote”完成一个进程,这是在该应用程序的用户id下完成的。将根任务保持为本机可执行文件或shell脚本以代表应用程序执行关键任务更简单。或者将它们作为特权系统服务添加到系统中。通常,最好的方法是创建一个unix组,并授予它访问特定功能(比如访问设备文件)的权限,并在该组中运行服务,或者将其与android许可证配对。实际上,没有任何方法来提升现有进程的权限,只意味着启动一个以root用户身份运行的新进程。例如,向su传递一个要执行的命令,请参见@Chris如何启动一个新的Activity/Task/Android java app/etc?这将需要对Android进行巨大的更改。通常情况下,您不会为android java执行创建进程,而是让android请求“zygote”完成一个进程,这是在该应用程序的用户id下完成的。将根任务保持为本机可执行文件或shell脚本以代表应用程序执行关键任务更简单。或者将它们作为特权系统服务添加到系统中。通常,最好的方法是创建一个unix组,并授予它访问特定功能(比如访问设备文件)的权限,然后在该组中运行服务,或者将其与android权限配对。您的代码块上似乎存在css错误。最后一个角色总是在第一个位置?!谢谢分享你的课程。我发现将命令作为参数传递更有用,例如boolean execute(ArrayList commands){…}我已经查找了近2天的此类信息(只有su下的命令行)。谢谢!谢谢,一个完美的片段:)@Muzikant您能解释一下这行中的255是什么意思吗:if(255!=supprocessretval)意思是什么?您的代码块上似乎有一个css错误。最后一个角色总是在第一个位置?!谢谢分享你的课程。我发现将命令作为参数传递更有用,例如boolean execute(ArrayList commands){…}我已经查找了近2天的此类信息(只有su下的命令行)。谢谢!谢谢,一个完美的片段:)@Muzikant你能解释一下255在t中的作用吗
public abstract class ExecuteAsRootBase
{
   public static boolean canRunRootCommands()
   {
      boolean retval = false;
      Process suProcess;

      try
      {
         suProcess = Runtime.getRuntime().exec("su");

         DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
         DataInputStream osRes = new DataInputStream(suProcess.getInputStream());

         if (null != os && null != osRes)
         {
            // Getting the id of the current user to check if this is root
            os.writeBytes("id\n");
            os.flush();

            String currUid = osRes.readLine();
            boolean exitSu = false;
            if (null == currUid)
            {
               retval = false;
               exitSu = false;
               Log.d("ROOT", "Can't get root access or denied by user");
            }
            else if (true == currUid.contains("uid=0"))
            {
               retval = true;
               exitSu = true;
               Log.d("ROOT", "Root access granted");
            }
            else
            {
               retval = false;
               exitSu = true;
               Log.d("ROOT", "Root access rejected: " + currUid);
            }

            if (exitSu)
            {
               os.writeBytes("exit\n");
               os.flush();
            }
         }
      }
      catch (Exception e)
      {
         // Can't get root !
         // Probably broken pipe exception on trying to write to output stream (os) after su failed, meaning that the device is not rooted

         retval = false;
         Log.d("ROOT", "Root access rejected [" + e.getClass().getName() + "] : " + e.getMessage());
      }

      return retval;
   }

   public final boolean execute()
   {
      boolean retval = false;

      try
      {
         ArrayList<String> commands = getCommandsToExecute();
         if (null != commands && commands.size() > 0)
         {
            Process suProcess = Runtime.getRuntime().exec("su");

            DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

            // Execute commands that require root access
            for (String currCommand : commands)
            {
               os.writeBytes(currCommand + "\n");
               os.flush();
            }

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

            try
            {
               int suProcessRetval = suProcess.waitFor();
               if (255 != suProcessRetval)
               {
                  // Root access granted
                  retval = true;
               }
               else
               {
                  // Root access denied
                  retval = false;
               }
            }
            catch (Exception ex)
            {
               Log.e("ROOT", "Error executing root action", ex);
            }
         }
      }
      catch (IOException ex)
      {
         Log.w("ROOT", "Can't get root access", ex);
      }
      catch (SecurityException ex)
      {
         Log.w("ROOT", "Can't get root access", ex);
      }
      catch (Exception ex)
      {
         Log.w("ROOT", "Error executing internal operation", ex);
      }

      return retval;
   }
   protected abstract ArrayList<String> getCommandsToExecute();
}