安卓(Android) 怎么获取已安装的应用程序

2019年10月17日21:34:28安卓(Android) 怎么获取已安装的应用程序已关闭评论 269 953字阅读3分10秒

1.简介

安卓(Android) 怎么获取已安装的应用程序?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

public class Main {
    public static PackageInfo[] getInstalledApps(Context context) {
        PackageManager pkgManager = context.getPackageManager();
        List<PackageInfo> packs = pkgManager.getInstalledPackages(0);
        List<PackageInfo> appList = new ArrayList<PackageInfo>();
        for (int i = 0; i < packs.size(); i++) {
            PackageInfo p = packs.get(i);
            Intent intent = pkgManager
                    .getLaunchIntentForPackage(p.packageName);
            if (intent != null) {
                appList.add(p);
            }
        }
        PackageInfo[] apps = new PackageInfo[appList.size()];
        appList.toArray(apps);
        Arrays.sort(apps, new Comparator<PackageInfo>() {

            @Override
            public int compare(PackageInfo lhs, PackageInfo rhs) {
                return lhs.packageName.compareToIgnoreCase(rhs.packageName);
            }

        });
        return apps;
    }
}

 
继续阅读
编程十万个怎么办