我希望在外部浏览器中打开一些链接。在打开之前,我想显示设备上的浏览器列表。我使用了Intent.ACTION_VIEW实现了这个功能。但根据我的应用程序要求,即使设备上只有一个浏览器应用程序,我也想显示浏览器列表。有人对此有什么想法吗?谢谢。
我希望在外部浏览器中打开一些链接。在打开之前,我想显示设备上的浏览器列表。我使用了Intent.ACTION_VIEW实现了这个功能。但根据我的应用程序要求,即使设备上只有一个浏览器应用程序,我也想显示浏览器列表。有人对此有什么想法吗?谢谢。
如果你只有一个浏览器应用程序,选择器将不会被启动,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);
请参考此文档
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);
}
设置>应用程序>清除默认值 (
如果您之前已将Chrome设置为“默认应用程序”,则需要执行此操作
这两张图片是我的来源:
选择浏览器或选择默认浏览器的消息: http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-03.jpg
清除默认浏览器,再次显示意图选择器: http://cdn2.ubergizmo.com/wp-content/uploads/2015/02/how-to-default-app-02.jpg
IntentChooser
并检索设备中的所有浏览器应用程序(在这种情况下只会有一个应用程序),然后显示它,参见我的编辑。 - Houcine