首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查看是否授予了root权限?

问查看是否授予了root权限?
EN

Stack Overflow用户
提问于 2012-11-24 02:51:02
回答 2查看 3.1K关注 0票数 2

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

代码语言:javascript
复制
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是否真的被授予了?

顺便说一下,我如何使用Runtime.getRuntime.exec()命令存储命令的输出?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-24 04:24:50

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

代码语言:javascript
复制
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 -命令为execute
  • results的字符串-空ArrayList以返回命令输出。如果为null,则不返回输出。

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

代码语言:javascript
复制
//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

致以问候。

票数 2
EN

Stack Overflow用户

发布于 2014-10-25 10:26:44

代码语言:javascript
复制
 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);
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13534419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档