Give the option to always choose a browser to open

2019-04-15 11:03发布

问题:

Is there a way to let the user always choose which application (browser) that should open a specific link? Similar to what happens if the user hasn't chosen the default program yet.

My code

            Intent intent = new Intent(Intent.ACTION_VIEW);
            forumintent.setData(Uri.parse(url));
            startActivity(intent);

回答1:

The following method works for all implicit intents - not limited to your question about browsers.

Generally. when you issue an implicit intent (like ACTION_VIEW), the host Android device will check to see whether there is a default app to handle the intent. If there is a default app, then, by default, android will automatically redirect to the app.

However, you can force an app chooser for implicit intents. For this, you need to use Intent.createChooser() method. See this example:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url)); // only used based on your example.

String title = "Select a browser";
// Create intent to show the chooser dialog
Intent chooser = Intent.createChooser(intent, title);

// Verify the original intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(chooser);
}