Home » Android » android – How can I dynamically detect whether my application is system or normal?

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

Posted by: admin May 14, 2020 Leave a comment

Questions:

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 to differentiate via code.

if(system app) {
  //do something
}
else{
   //do nothing
}
How to&Answers:

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?
    }
}

Answer:

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;

Answer:

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

Answer:

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
    }
}

Answer:

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/” .