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

前端 未结 5 2104
失恋的感觉
失恋的感觉 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:13

    For starters, you cant uninstall a system app but you can uninstall a normal app using "Settings > Applications > Manage Applications".

    0 讨论(0)
  • 2021-02-14 12:18

    You could try using the flags available in the ApplicationInfo class (android.conent.pm). For example:

    ...
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
    
    for (ApplicationInfo ai: installedApps) {
    
        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            // System app - do something here
            ...
        } else {
            // User installed app?
        }
    }
    
    0 讨论(0)
  • 2021-02-14 12:22

    Forget PackageManager! You only asked about your own application. Within your Activity#onCreate(Bundle) you can just call getApplicationInfo() and test its flags like this:

    boolean isSystemApp = (getApplicationInfo().flags
      & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
    
    0 讨论(0)
  • 2021-02-14 12:24

    I see a comprehensive answer by Pankaj Kumar in this SO link : "How do I check if an app is a non-system app in Android?" OR in this blog by him : "http://pankajchunchun.wordpress.com/2014/07/08/how-to-check-if-application-is-system-app-or-not-by-signed-signature/" .

    0 讨论(0)
  • 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<ApplicationInfo> allApps = pm.getInstalledApplications(0);
    
    for (ApplicationInfo ai: allApps) {
    
        if (isUserApp(ai,true)) {
            // User app or Updated SystemApp - do something here
            ...
        } else {
            // System app
        }
    }
    
    0 讨论(0)
提交回复
热议问题