Determine if running on a rooted device

后端 未结 24 2225
無奈伤痛
無奈伤痛 2020-11-22 06:43

My app has a certain piece of functionality that will only work on a device where root is available. Rather than having this feature fail when it is used (and then show an a

24条回答
  •  盖世英雄少女心
    2020-11-22 07:04

    Further to @Kevins answer, I've recently found while using his system, that the Nexus 7.1 was returning false for all three methods - No which command, no test-keys and SuperSU was not installed in /system/app.

    I added this:

    public static boolean checkRootMethod4(Context context) {
        return isPackageInstalled("eu.chainfire.supersu", context);     
    }
    
    private static boolean isPackageInstalled(String packagename, Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (NameNotFoundException e) {
            return false;
        }
    }
    

    This is slightly less useful in some situations (if you need guaranteed root access) as it's completely possible for SuperSU to be installed on devices which don't have SU access.

    However, since it's possible to have SuperSU installed and working but not in the /system/app directory, this extra case will root (haha) out such cases.

提交回复
热议问题