Get list of installed android applications

前端 未结 1 1499
陌清茗
陌清茗 2020-11-27 03:14

Hi I want to get a list of all of the installed applications on the users device I have been googling for the longest time but can\'t find what i want this link was the clos

相关标签:
1条回答
  • 2020-11-27 04:01

    I was working on something like this recently. One thing I'll say up front is to be sure and perform this in a separate thread -- querying the application information is SLOW. The following will get you a list of ALL the installed applications. This will include a lot of system apps that you probably aren't interested in.

    PackageManager pm = getPackageManager();
    List<ApplicationInfo> apps = pm.getInstalledApplications(0);
    

    To limit it to just the user-installed or updated system apps (e.g. Maps, GMail, etc), I used the following logic:

    List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
    
    for(ApplicationInfo app : apps) {
        //checks for flags; if flagged, check if updated system app
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
            installedApps.add(app);
        //it's a system app, not interested
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
            //Discard this one
        //in this case, it should be a user-installed app
        } else {
            installedApps.add(app);
        }
    }
    

    EDIT: Also, to get the name and icon for the app (which is probably what takes the longest -- I haven't done any real deep inspection on it -- use this:

    String label = (String)pm.getApplicationLabel(app);
    Drawable icon = pm.getApplicationIcon(app);
    

    installedApps should have a full list of the apps you need, now. Hope this helps, but you may have to modify the logic a bit depending on what apps you need to have returned. Again, it is SLOW, but it's just something you have to work around. You might want to build a data cache in a database if it's something you'll be accessing frequently.

    0 讨论(0)
提交回复
热议问题