Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

arrays - Get list of installed android applications

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 closest though and works fine except me being new don't understand how to use the method getPackages(); and create a list with it

http://www.androidsnippets.com/get-installed-applications-with-name-package-name-version-and-icon

Any help on how to create the actual list would be a major help i have all that code already in just can't get the list to actually show thanks for any help

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...