【问题标题】:Android: search installed package using intent-filterAndroid:使用意图过滤器搜索已安装的包
【发布时间】:2014-03-12 23:32:08
【问题描述】:

我正在尝试列出包含特定意图过滤器的已安装包

例如:

意图过滤器 1

Intent intent1 = new Intent("com.this.is.sample1");

意图过滤器 2

Intent intent2 = new Intent("com.this.is.sample2");
intent2.addCategory("com.this.is.sample2.category");

意图过滤器 3

Intent intent3 = new Intent("com.this.is.sample3");
intent3.addCategory("com.this.is.sample3.category");

我的问题

如何在不重复的情况下将它们全部列在 1 个列表中?

目前我正在使用此代码,但它会重复

List<ResolveInfo> listone, listtwo, listthree, listfinal;
listone = getPackageManager().queryIntentActivities(intent1, PackageManager.GET_RESOLVED_FILTER);

listtwo = getPackageManager().queryIntentActivities(intent2, PackageManager.GET_RESOLVED_FILTER);

listthree = getPackageManager().queryIntentActivities(intent3, PackageManager.GET_RESOLVED_FILTER);

//Adding all of them into 1 List<ResolveInfo>
listfinal.addAll(listone);
listfinal.addAll(listtwo);
listfinal.addAll(listthree);

新代码,仍然包含重复项

Set<ResolveInfo> newlist = new HashSet<ResolveInfo>();
List<ResolveInfo> listone, listtwo, listthree, listfinal;

listone = getPackageManager().queryIntentActivities(intent1, PackageManager.GET_RESOLVED_FILTER);

listtwo = getPackageManager().queryIntentActivities(intent2, PackageManager.GET_RESOLVED_FILTER);

listthree = getPackageManager().queryIntentActivities(intent3, PackageManager.GET_RESOLVED_FILTER);

if (listone!=null) {
    for (int i = 0; i < listone.size(); i++) {
        newlist.add(listone.get(i));
    }

    if (listtwo!=null) {
        for (int j = 0; j < listtwo.size(); j++) {
            newlist.add(listtwo.get(j));
        }

        if (listthree!=null) {
            for (int k = 0; k < listthree.size(); k++) {
                newlist.add(listthree.get(k));
            }

            if (newlist!=null) {
                listfinal.addAll(newlist);
            }
        }
    }
}

【问题讨论】:

标签: android android-intent package intentfilter


【解决方案1】:

什么算作重复?

您当前的 3 个列表属于 ResolveInfo,并且您特别要求填写其中的过滤器字段。因此,您很可能会在每个列表中返回相同的包,但使用不同的过滤器导致匹配。

假设您想要一个没有重复的包名称列表,您将需要自己进行重复数据删除 - 其中填充一组包名称可能是完成此操作的最佳方法。

【讨论】:

  • 示例 1 应用程序具有上述 2 个甚至全部的意图过滤器,这是重复的。看看上面,我已经更新了代码但仍然包含重复
  • 您的新代码仍在处理 ResolveInfo 对象。您应该构建一组包名称,而不是 ResolveInfo 对象。
猜你喜欢
  • 2011-06-04
  • 2011-08-09
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-02-10
相关资源
最近更新 更多