Give the option to always choose a browser to open a link

£可爱£侵袭症+ 提交于 2020-01-13 06:13:08

问题


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);
}


来源:https://stackoverflow.com/questions/28492579/give-the-option-to-always-choose-a-browser-to-open-a-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!