Search code examples
androidandroid-intent

Custom filtering of intent chooser based on installed Android package name


I'd like to leverage the built-in intent chooser to display a custom filtered list of apps for user to select from and launch.

I know how to get a list of installed packages:

final Intent myIntent = new Intent(android.content.Intent.ACTION_MAIN);  
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(myIntent, 0);

At this point I want to filter the list based on a specific string (or variation of strings) contained within the package name, which I can figure out how to do as well.

But here's where I get stuck. As far as I know, Intent.createChooser() takes only a single target Intent as a parameter. I was hoping there was an overload that took a list of intents based on package and class names or something. But I don't see anything like that. Did I miss that somewhere?

So the question is, is this possible to do with a built-in chooser, or do I have to construct my own with AlertDialog Builder? I'm hoping to avoid the later.

Thanks in advance.


Solution

  • The only one additional parameter for the chooser is Intent.EXTRA_INITIAL_INTENTS. Its description is:

    A Parcelable[] of Intent or LabeledIntent objects as set with putExtra(String, Parcelable[]) of additional activities to place a the front of the list of choices, when shown to the user with a ACTION_CHOOSER.

    I haven't found any way in Android sources to exclude other activities from the list, so it seems there's no way to do what you want to do using the chooser.

    EDIT: That's really easy to find out. Just check ChooserActivity and ResolverActivity source code. These classes are rather small.