How do I check if an app is a non-system app in Android?

后端 未结 13 1272
无人共我
无人共我 2020-11-28 22:51

I am getting a list of ApplicationInfo Objects with packageManager.getInstalledApplications(0) and attempting to categorize them by whether or not they are a sy

13条回答
  •  余生分开走
    2020-11-28 23:22

    You can check the signature of application which it signed with system. Like below

    /**
     * Match signature of application to identify that if it is signed by system
     * or not.
     * 
     * @param packageName
     *            package of application. Can not be blank.
     * @return true if application is signed by system certificate,
     *         otherwise false
     */
    public boolean isSystemApp(String packageName) {
        try {
            // Get packageinfo for target application
            PackageInfo targetPkgInfo = mPackageManager.getPackageInfo(
                    packageName, PackageManager.GET_SIGNATURES);
            // Get packageinfo for system package
            PackageInfo sys = mPackageManager.getPackageInfo(
                    "android", PackageManager.GET_SIGNATURES);
            // Match both packageinfo for there signatures
            return (targetPkgInfo != null && targetPkgInfo.signatures != null && sys.signatures[0]
                    .equals(targetPkgInfo.signatures[0]));
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    

    You can get more code on my blog How to check if application is system app or not (By signed signature)

提交回复
热议问题