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

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

    I was under the impression that all apps in the system image are system apps (and normally installed in /system/app).

    If FLAG_SYSTEM is only set to system applications, this will work even for apps in external storage:

    boolean isUserApp(ApplicationInfo ai) {
        int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
        return (ai.flags & mask) == 0;
    }
    

    An alternative is to use the pm command-line program in your phone.

    Syntax:

    pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]
    
    pm list packages: prints all packages, optionally only
      those whose package name contains the text in FILTER.  Options:
        -f: see their associated file.
        -d: filter to only show disbled packages.
        -e: filter to only show enabled packages.
        -s: filter to only show system packages.
        -3: filter to only show third party packages.
        -i: see the installer for the packages.
        -u: also include uninstalled packages.
    

    Code:

    ProcessBuilder builder = new ProcessBuilder("pm", "list", "packages", "-s");
    Process process = builder.start();
    
    InputStream in = process.getInputStream();
    Scanner scanner = new Scanner(in);
    Pattern pattern = Pattern.compile("^package:.+");
    int skip = "package:".length();
    
    Set systemApps = new HashSet();
    while (scanner.hasNext(pattern)) {
        String pckg = scanner.next().substring(skip);
        systemApps.add(pckg);
    }
    
    scanner.close();
    process.destroy();
    

    Then:

    boolean isUserApp(String pckg) {
        return !mSystemApps.contains(pckg);
    }
    

提交回复
热议问题