How to choose any app to launch with the app chooser in Android?

981 Views Asked by At

The following code is essentially copied from Android document:

Intent intent = new Intent(Intent.ACTION_SEND);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, "Choose an app");
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}

Unfortunately, the app chooser does not show up. Could anyone point out what is wrong with the above code?

I want to use the chooser to allow the user to select an app, and the app to remember this app, and launch this later in another activity in code,

1

There are 1 best solutions below

9
On BEST ANSWER

Your Intent does not match any app on the device. My guess is that it is the missing MIME type. Call setType() on intent, passing in MIME type of the content that you are trying to share.

Your bigger problem is that you are not actually sharing anything (no EXTRA_TEXT, nor EXTRA_STREAM), and so you may crash whatever activity responds to your Intent.

UPDATE: Based upon your updated question, you seem to be taking "chooser" too literally. :-) While the chooser allows the user to choose from one of several possible matching activities, the chooser then starts the chosen activity. On newer versions of Android (5.1+, IIRC), there are ways that you can find out that what the chooser chose. But if your objective is not to start an activity, but just to have the user choose one... AFAIK, you need to roll your own UI for that, based on PackageManager and queryIntentActivities(). That's basically what a home screen does: find all activities implementing ACTION_MAIN/CATEGORY_LAUNCHER, then display them for the user to choose from. My Launchalot sample app demonstrates this process.