How can I dynamically detect whether my application is system or normal?

前端 未结 5 2090
失恋的感觉
失恋的感觉 2021-02-14 11:53

How do I differentiate between a system app and a normal app ? I looked through android PackageManager and could not find any.

Edit: I want

5条回答
  •  爱一瞬间的悲伤
    2021-02-14 12:29

    A simple function:

    public boolean isUserApp(ApplicationInfo ai,boolean getUpdatedSystemApps){      
        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            if(getUpdatedSystemApps==true){
                if((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0){
                    return true;
                } else {
                    return false;
                }                  
            }
            return false;
        } else {
            return true;
        }
    }
    

    you can use above function like:

    PackageManager pm = getPackageManager();
    List allApps = pm.getInstalledApplications(0);
    
    for (ApplicationInfo ai: allApps) {
    
        if (isUserApp(ai,true)) {
            // User app or Updated SystemApp - do something here
            ...
        } else {
            // System app
        }
    }
    

提交回复
热议问题