2

我已将我的应用程序设置为su使用以下代码执行命令:

try {
            Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            altDialog.setTitle("No Root");
            altDialog
                    .setMessage("I am afraid I have been unable to execute the su binary. Please check your root status.");
            altDialog.setCancelable(false);
            altDialog.setButton("Exit App",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // TODO Auto-generated method stub
                            Log.e("Android .img Flasher",
                                    "Exiting due to root error");
                            finish();
                        }
                    });
        }

如果su命令不存在(我相信),这会捕获,但如果 root 确实被授予,则不会。

我将如何检查是否确实授予了 root 权限?

附带说明一下,我如何能够使用Runtime.getRuntime.exec()命令存储命令的输出?

4

2 回答 2

2

您可以使用下面的代码。我为通用命令编写了它,但它也适用于su命令。如果命令成功以及命令输出(或错误),它将返回。

public static boolean execCmd(String command, ArrayList<String> results){
    Process process;
    try {
        process = Runtime.getRuntime().exec(new String [] {"sh", "-c", command});
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } 

    int result;
    try {
        result = process.waitFor();
    } catch (InterruptedException e1) {
        e1.printStackTrace();
        return false;
    }

    if(result != 0){ //error executing command
        Log.d("execCmd", "result code : " + result);
        String line; 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
        try {
            while ((line = bufferedReader.readLine()) != null){
                if(results != null) results.add(line);
                Log.d("execCmd", "Error: " + line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    //Command execution is OK
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); 

    String line; 
    try {
        while ((line = bufferedReader.readLine()) != null){
            if(results != null) results.add(line);
            Log.d("execCmd", line);
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

你用两个参数调用它:

  • command- 带有要执行的命令的字符串
  • results- 清空 ArrayList 以返回命令输出。如果为 null,则不返回输出。

要检查su命令,您可以执行以下操作:

//Array list where the output will be returned
ArrayList<String> results = new ArrayList<String>();
//Command to be executed
String command = "su -c ls";
boolean result = execCmd(command,results);
//result returns command success
//results returns command output

问候。

于 2012-11-23T20:24:49.663 回答
0
 public static boolean isRootAvailable(){
            Process p = null;
            try{
               p = Runtime.getRuntime().exec(new String[] {"su"});
               writeCommandToConsole(p,"exit 0");
               int result = p.waitFor();
               if(result != 0)
                   throw new Exception("Root check result with exit command " + result);
               return true;
            } catch (IOException e) {
                Log.e(LOG_TAG, "Su executable is not available ", e);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Root is unavailable ", e);
            }finally {
                if(p != null)
                    p.destroy();
            }
            return false;
        }
 private static String writeCommandToConsole(Process proc, String command, boolean ignoreError) throws Exception{
            byte[] tmpArray = new byte[1024];
            proc.getOutputStream().write((command + "\n").getBytes());
            proc.getOutputStream().flush();
            int bytesRead = 0;
            if(proc.getErrorStream().available() > 0){
                if((bytesRead = proc.getErrorStream().read(tmpArray)) > 1){
                    Log.e(LOG_TAG,new String(tmpArray,0,bytesRead));
                    if(!ignoreError)
                        throw new Exception(new String(tmpArray,0,bytesRead));
                }
            }
            if(proc.getInputStream().available() > 0){
                bytesRead = proc.getInputStream().read(tmpArray);
                Log.i(LOG_TAG, new String(tmpArray,0,bytesRead));
            }
            return new String(tmpArray);
        }
于 2014-10-25T02:26:43.610 回答