安卓(Android) 怎么判断是不是错误的Root

2019年10月13日14:58:19安卓(Android) 怎么判断是不是错误的Root已关闭评论 337 1924字阅读6分24秒

1.简介

安卓(Android) 怎么判断是不是错误的Root?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class Main{
    private static final String SU_PATH = "/system/bin/su";
    private static final String SU_PATH_X = "/system/xbin/su";
    public static boolean isWrongRoot() {
        
        String suStat = runCommand("ls -l " + SU_PATH, false).result
                + runCommand("ls -l " + SU_PATH_X, false).result;
        if (GlobalInstance.DEBUG) {
            Log.e("suStat", suStat);
        }
        return ((!suStat.contains("-rwsr-sr-x")) && (!suStat
                .contains("-rwsr-xr-x")));
    }
    @SuppressWarnings("deprecation")
    public static CommandResult runCommand(String command, boolean root) {
        if (GlobalInstance.DEBUG) {
            Log.e("runRootCommand", command);
        }
        Process process = null;
        DataOutputStream os = null;
        DataInputStream stdout = null;
        DataInputStream stderr = null;
        CommandResult ret = new CommandResult();
        try {
            StringBuffer output = new StringBuffer();
            StringBuffer error = new StringBuffer();
            if (root) {
                process = Runtime.getRuntime().exec("su");
                os = new DataOutputStream(process.getOutputStream());
                os.writeBytes(command + "\n");
                os.writeBytes("exit\n");
                os.flush();
            } else {
                process = Runtime.getRuntime().exec(command);
            }
            stdout = new DataInputStream(process.getInputStream());
            String line;
            while ((line = stdout.readLine()) != null) {
                output.append(line).append('\n');
            }
            stderr = new DataInputStream(process.getErrorStream());
            while ((line = stderr.readLine()) != null) {
                error.append(line).append('\n');
            }
            process.waitFor();
            ret.result = output.toString().trim();
            ret.error = error.toString().trim();
        } catch (Exception e) {
            ret.result = "";
            ret.error = e.getMessage();
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (stdout != null) {
                    stdout.close();
                }
                if (stderr != null) {
                    stderr.close();
                }
                
                
                
                
                
                
                
            } catch (Exception e) {
                ret.result = "";
                ret.error = e.getMessage();
            }
        }
        if (GlobalInstance.DEBUG) {
            Log.e("runRootCommand-Result", String.format(
                    "result:%s, error:%s", ret.result, ret.error));
        }
        return ret;
    }
}

 
继续阅读
编程十万个怎么办