33

我正在编写一个应用程序,允许用户查看已安装应用程序的列表,选择其中一个,然后按计划启动它。使用来自 stackoverflow 的教程,我设法弄清楚如何获取已安装活动的列表、它们的包名称和图标(即这里- 几种方法)。以防万一,这就是我开始活动的方式,它完美无缺,在这里没问题:

Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
launchIntent.setAction(Intent.ACTION_MAIN);
launchIntent.addCategory(Intent.CATEGORY_LAUNCHER);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launchIntent);

问题在于检索已安装应用程序的列表。我找到了两种获取已安装应用程序列表的方法:

1) 使用

PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplication(PackageManager.GET_META_DATA) 

并从apps您的每个元素中获取它的包名和包标签(应用程序名称)。

2) 使用

PackageManager pm = getPackageManager();    
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mainIntent, 0);
for(ResolveInfo info : resolveInfos) {
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
    //...
    //get package name, icon and label from applicationInfo object    
}

一种方法有一个问题:它返回所有已安装的包,包括系统服务,这些包可能不包含任何活动,因此无法启动。这是带有示例的屏幕截图: 带有软件包的应用程序列表

上面所有没有图标的项目都不能启动。

第二种方法也有一个问题:列表中的几个项目有重复项: 具有重复项的应用程序列表

当我在调试器中设置断点时,我看到这些“地图”项目具有不同的活动名称(“com.google.android.maps.MapsActivity”、“com.google.android.maps.LatitudeActivity”、“com.google .android.maps.PlacesActivity”等)。

我决定使用第二种方法,因为它提供了一个更适合我需要的列表,但我找不到过滤掉重复项的方法,只显示应用程序的默认活动,因为它们出现在启动器中(您在手机的应用程序列表中只能看到一个“地图”,而不是四个)。我尝试通过 过滤掉系统应用程序ApplicationInfo.FLAG_SYSTEM,但这会删除许多我想要拥有的应用程序,包括地图和其他预装的应用程序。我PackageManager.MATCH_DEFAULT_ONLY在执行 queryIntentActivities 时尝试过使用标志,但这也会过滤掉许多应用程序,只留下一些。

我有点迷路了,我不知道该怎么办。我已阅读 stackoverflow 上有关检索已安装应用程序列表的所有问题,但从未提出过这个问题。请帮助任何人?如何检索没有重复的已安装可启动应用程序列表?

4

5 回答 5

15
Intent localIntent2 = new Intent(Intent.ACTION_PICK_ACTIVITY);
Intent localIntent3 = new Intent(Intent.ACTION_MAIN, null);
localIntent3.addCategory(Intent.CATEGORY_LAUNCHER); 
localIntent2.putExtra(Intent.EXTRA_INTENT, localIntent3);
startActivityForResult(localIntent2, 1);

试试这个代码。它将仅列出所有安装在您设备中的应用程序。

于 2012-03-28T13:26:14.137 回答
14

我可能迟到了,但我刚刚找到了一种让所有应用程序都具有启动器且没有重复应用程序(包括联系人、地图等系统应用程序)的完美方法。虽然,Satheesh 的答案可能有效(我自己没有检查过),但我想选择多个活动,所以我使用下面的代码来安装应用程序。

我使用了您的第二种方法并使用 HashSet 丢弃了重复的包。这是最终代码:

    final PackageManager packageManager = getPackageManager();
    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
    //using hashset so that there will be no duplicate packages, 
    //if no duplicate packages then there will be no duplicate apps
    HashSet<String> packageNames = new HashSet<String>(0);
    List<ApplicationInfo> appInfos = new ArrayList<ApplicationInfo>(0);

    //getting package names and adding them to the hashset
    for(ResolveInfo resolveInfo : resInfos) {
        packageNames.add(resolveInfo.activityInfo.packageName);
    }

    //now we have unique packages in the hashset, so get their application infos
    //and add them to the arraylist
    for(String packageName : packageNames) {
        try {
            appInfos.add(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
        } catch (NameNotFoundException e) {
            //Do Nothing
        }
    }

    //to sort the list of apps by their names
    Collections.sort(appInfos, new ApplicationInfo.DisplayNameComparator(packageManager));
于 2014-06-22T13:17:00.613 回答
6

试试下面的代码,让我知道发生了什么。

PackageManager manager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> resolveInfos= manager.queryIntentActivities(mainIntent, 0);
// Below line is new code i added to your code
Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(manager));

for(ResolveInfo info : resolveInfos) {
     ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;
     //...
     //get package name, icon and label from applicationInfo object    
}
于 2012-03-28T10:17:02.130 回答
6

@Ashish Tanna 和 jozze 都是对的,但性能可能有点问题。

这是最好的表现之一。

Set<String> packageNameSet = new HashSet<>();
PackageManager pm = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(mainIntent, 0);
for(ResolveInfo info : resolveInfos) {
    // be added
    ApplicationInfo applicationInfo;
    if (info == null || (applicationInfo = info.activityInfo.applicationInfo) == null
            || !applicationInfo.enabled || packageNameSet.contains(applicationInfo.packageName)) {
        continue;
    }
    packageNameSet.add(applicationInfo.packageName);

    //...
    //get package name, icon and label from applicationInfo object
}

(1)添加一个HashSet
(2)判断应用是否开启
(3)判断hashset里面是否有

于 2016-11-27T09:11:19.050 回答
2

我有同样的要求。最终,我添加了另一个条件来过滤应用列表。我刚刚检查了该应用程序是否具有“启动器意图”。

所以,结果代码看起来像......

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

for (ApplicationInfo app : apps) {
    if(pm.getLaunchIntentForPackage(app.packageName) != null) {
        // apps with launcher intent
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
            // updated system apps
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            // system apps
        } else {
            // user installed apps
        }
        appsList.add(app);
    }

}
于 2015-04-24T19:07:01.907 回答