在安卓系统中打开链接时显示浏览器列表

6

我希望在外部浏览器中打开一些链接。在打开之前,我想显示设备上的浏览器列表。我使用了Intent.ACTION_VIEW实现了这个功能。但根据我的应用程序要求,即使设备上只有一个浏览器应用程序,我也想显示浏览器列表。有人对此有什么想法吗?谢谢。

3个回答

14

如果你只有一个浏览器应用程序,选择器将不会被启动,URL 将在该浏览器应用程序中加载。如果你有两个或更多的浏览器应用程序,Android 系统会启动一个 IntentChooser 来显示设备上安装的所有浏览器应用程序的列表,因此用户可以选择他喜欢的浏览器来加载 URL:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
startActivity(intent); 

编辑: 创建您自己的意图选择器的方法:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));

// Always use string resources for UI text. This says something like "Share this photo with"
String title = getString(R.string.chooser_title);
// Create and start the chooser
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);

请参考此文档


1
谢谢您的回复。但我的要求是,即使在Android设备中只有一个浏览器应用程序,我也想显示IntentChooser。请给予建议。 - MithunRaj
1
@MithunRaj 你可以创建自己的 IntentChooser 并检索设备中的所有浏览器应用程序(在这种情况下只会有一个应用程序),然后显示它,参见我的编辑。 - Houcine

3
Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.maidofknowledge.com"));
            Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
            if (sendIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(chooser);
            }

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接