[Android] System App的判斷方法

這個函數會列出所有的System app,包含/system/app/底下的以及/data/app/底下的更新檔。
FLAG_SYSTEM simply gets the apps that are installed in the device's system image, and this will work even for apps in external storage(ex: SD card):
void SystemAppChecking (Context context) {
    PackageManager pm = context.getPackageManager();
    List<PackageInfo> list = pm.getInstalledPackages(0);

    for(PackageInfo pi : list) {
       ApplicationInfo ai = null;
        try {
            ai = pm.getApplicationInfo(pi.packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (ai != null) {
            System.out.println("The packages is" + ai.packageName);
            if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                System.out.println(ai.processName + " is a System APP.");
            } else {
                System.out.println(ai.processName + " is NOT a System APP.");
            }
        }
    }
}
Reference: http://stackoverflow.com/questions/8784505/how-do-i-check-if-an-app-is-a-non-system-app-in-android

留言