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

后端 未结 13 1187
无人共我
无人共我 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:30

    Well, it's a sloppy solution in my opinion (what if /data/app isn't the apps directory on all devices?), but after a thorough search, this is what I have come up with:

    for (ApplicationInfo ai : appInfo) {
        if (ai.sourceDir.startsWith("/data/app/")) {
            //Non-system app
        }
        else {
            //System app
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:30

    If an Application is a non-system application it must have a launch Intent by which it can be launched. If the launch intent is null then its a system App.

    Example of System Apps: "com.android.browser.provider", "com.google.android.voicesearch".

    For the above apps you will get NULL when you query for launch Intent.

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    for(ApplicationInfo packageInfo:packages){
        if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
                    String currAppName = pm.getApplicationLabel(packageInfo).toString();
                   //This app is a non-system app
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:32

    There is a little bit of misunderstanding here. For Android the notion of a "system app" is one that is install on the system image, it says nothing about what developer it came from. So, if an OEM decides to preload Facebook on to the system image, it is a system app and will continue to be so, regardless of where updates to the app get installed. They won't get installed on the system image, for sure, because it is read-only.

    So ApplicationInfo.FLAG_SYSTEM is correct, but that doesn't seem to be the question you are asking. I think you're asking if a package is signed with the system certificate. Which is not necessarily a good indicator of anything, this may vary from device to device and some surprising components on vanilla Android are not signed with the system certificate, even though you might expect them to be.

    In newer versions of Android there is a new path, /system/priv-app/ that attempts to be the install location for "real" system apps. Apps that are just pre-loaded on the system image then end up in /system/app/. See AOSP Privileged vs System app

    0 讨论(0)
  • 2020-11-28 23:33
    PackageManager pm = mcontext.getPackageManager();
    List<PackageInfo> list = pm.getInstalledPackages(0);
    
    for(PackageInfo pi : list) {
        ApplicationInfo ai = pm.getApplicationInfo(pi.packageName, 0);
    
        System.out.println(">>>>>>packages is<<<<<<<<" + ai.publicSourceDir);
    
        if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            System.out.println(">>>>>>packages is system package"+pi.packageName);          
        }
    }
    
    0 讨论(0)
  • 2020-11-28 23:36

    If having an APK file and want to check is it System app or User installed a Simple logic:- System app Files are not writable

    private boolean isSystemApkFile(File file){
       return !file.canWrite();
    }
    
    0 讨论(0)
  • 2020-11-28 23:38
    if (!packageInfo.sourceDir.toLowerCase().startsWith("/system/"))
    
    0 讨论(0)
提交回复
热议问题