0

I'm trying to list installed package which contains specific intent-filter

For Example :

Intent-Filter 1

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

Intent-Filter 2

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

Intent-Filter 3

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

MY QUESTION

How to list all of them inside 1 List without having a duplicate?

CURRENTLY I'M USING THIS CODE BUT IT WILL MAKE DUPLICATE

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);

NEW CODE, STILL CONTAINS DUPLICATE

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);
            }
        }
    }
}
user3273595
  • 83
  • 1
  • 1
  • 10

1 Answers1

0

What counts as a duplicate?

Your current 3 lists are of ResolveInfo, and you specifically ask for the filter field in this to be filled in. Thus you may well get back the same package in each list, but with a different filter which caused the match.

Assuming you want a list of just package names without duplicates, you are going to need to do the deduplication yourself - where populating a Set of the package names may be the best way to accomplish this.

zmarties
  • 4,809
  • 22
  • 39
  • Example 1 application have those 2 or even all of intent-filter above, that's make duplicate. Take a look above, I have updated the code but still contains duplicate – user3273595 Mar 12 '14 at 15:34
  • Your new code is still dealing with ResolveInfo objects. You should be building a Set of package names, not ResolveInfo objects. – zmarties Mar 12 '14 at 16:08