has Root - Android Android OS

Android examples for Android OS:Root

Description

has Root

Demo Code


//package com.java2s;

import java.io.File;

public class Main {
    private static final String SU_PATH = "/system/bin/su";
    private static final String SU_PATH_X = "/system/xbin/su";
    private static final String APP_PATH = "/system/app/";

    /**// w  w  w .jav  a2s .  c  o m
     * 
     * @return |0:no root|1:find only su|2:find two|
     */
    public static int hasRoot() {
        boolean hasSU = findSU();
        if (!hasSU) {
            return 0;
        }
        boolean hasSuperUser = findSuperUser();
        return hasSuperUser ? 2 : 1;
    }

    private static boolean findSU() {
        boolean ret = openFile(SU_PATH).exists();
        if (!ret) {
            ret = openFile(SU_PATH_X).exists();
        }
        return ret;
    }

    private static boolean findSuperUser() {
        File apps = new File(APP_PATH);
        String[] apks = apps.list();
        boolean hasSuperUser = false;
        if (apks != null) {
            if (apks.length > 0) {
                for (String apk : apks) {
                    if (apk.toLowerCase().contains("superuser.apk")) {
                        hasSuperUser = true;
                        break;
                    }
                }
            }
        }
        return hasSuperUser;
    }

    private static File openFile(String path) {
        return new File(path);
    }
}

Related Tutorials