提供始终选择浏览器打开 link 的选项
Give the option to always choose a browser to open a link
有没有办法让用户始终选择应打开特定 link 的应用程序(浏览器)?类似于用户尚未选择默认程序时发生的情况。
我的代码
Intent intent = new Intent(Intent.ACTION_VIEW);
forumintent.setData(Uri.parse(url));
startActivity(intent);
以下方法适用于所有隐式意图 - 不限于您关于浏览器的问题。
一般。当您发出隐式意图(如 ACTION_VIEW
)时,主机 Android 设备将检查是否有默认应用程序来处理该意图。如果有默认应用程序,那么默认情况下,android 将自动重定向到该应用程序。
但是,您可以针对隐式意图强制应用选择器。为此,您需要使用 Intent.createChooser()
方法。看这个例子:
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);
}
有没有办法让用户始终选择应打开特定 link 的应用程序(浏览器)?类似于用户尚未选择默认程序时发生的情况。
我的代码
Intent intent = new Intent(Intent.ACTION_VIEW);
forumintent.setData(Uri.parse(url));
startActivity(intent);
以下方法适用于所有隐式意图 - 不限于您关于浏览器的问题。
一般。当您发出隐式意图(如 ACTION_VIEW
)时,主机 Android 设备将检查是否有默认应用程序来处理该意图。如果有默认应用程序,那么默认情况下,android 将自动重定向到该应用程序。
但是,您可以针对隐式意图强制应用选择器。为此,您需要使用 Intent.createChooser()
方法。看这个例子:
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);
}