java – Is there a way to explicitly say to android system resolve my intent to default app/activity for current intent action?-ThrowExceptions

Exception or error:

I need to open web link from my android application, but without prompt to user chooser dialog also known as “disambiguation dialog”, just use default browser to handle this intent.

When I use this code:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

there is no guarantee that resolving intent will be result in only one activity, according to description of Intent.resolveActivity method in official documentation:

…If multiple activities are found to satisfy the intent, the one with the highest priority will be used. If there are multiple activities with the same priority, the system will either pick the best activity based on user preference, or resolve to a system class that will allow the user to pick an activity and forward from there.

How to avoid chooser dialog and just pass to “best” activity or maybe default activity for this action type?

How to solve:

You can resolve the Intent yourself by calling PackageManager.queryIntentActivities().

If this returns only 1 result, then you can just call startActivity() and you can be sure that Android won’t show the chooser dialog.

If it returns more than 1 result, you can do something else. For example, since the results are returned from “best” to “worst” (whatever that means), you could just launch the first result explicitly. Or you could scan the results list looking for something that your code deems is appropriate (ie: matches a list of browser names).

Leave a Reply

Your email address will not be published. Required fields are marked *