Android 检测设备是否root

665 阅读1分钟

Android 检测设备是否root

本篇文章主要搜集下判断android 手机是否root的方法,可能判断的不是很准确,大家有疑问及时沟通回复,文章也会实时的更新.

package com.test.detection;

import java.io.DataOutputStream;
import java.io.File;

/**
 * @Author: zh
 * @Time: 23-11-28.
 * @Email:
 * @Describe:检测是否root
 */
public class RootUtils {

    public static boolean isDeviceRooted2() {
        String buildTags = android.os.Build.TAGS;
        if (buildTags != null && buildTags.contains("test-keys")) {
            return true;
        }
        File file = new File("/system/app/Superuser.apk");
        if (file.exists()) {
            return true;
        }
        String su = "su";
        String[] locations = {"/sbin/", "/system/bin/", "/system/xbin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/"};
        for (String location : locations) {
            if (new File(location + su).exists()) {
                return true;
            }
        }
        return false;
    }


    public static boolean isDeviceRooted(){
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("exit\n");
            os.flush();
            int exitValue = process.waitFor();
            return exitValue == 0;
        } catch (Exception e) {
            // 应用未获取root权限
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
        return false;
    }

}