Answer the question
In order to leave comments, you need to log in
What is the correct way to check for an application that can process an intent?
The documentation first shows this check for the presence of an intent processing application:
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(mapIntent);
}
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
Answer the question
In order to leave comments, you need to log in
The difference will be when this intent can be processed by different applications.
The first method will only show a selection dialog if there is no default application assigned to that intent. The second method will each time prompt the user to select an application.
Which method to choose depends on the business logic of this method. For example, if the button is needed to send an email, the first method is preferable (since the user usually uses one email client all the time); if it is a Share button, then it is more logical to offer the user each time to choose how exactly he wants to share the content.
If you only need to check that the application exists, then there is no difference (but the second method seems to me a little clearer and easier to understand). The only difference between them will be that one will return *all* suitable applications, and the other - one (the most suitable), if it exists.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question