use*_*421 2 android android-intent
有没有办法让用户始终选择应该打开特定链接的应用程序(浏览器)?与用户尚未选择默认程序时发生的情况类似.
我的代码
Intent intent = new Intent(Intent.ACTION_VIEW);
forumintent.setData(Uri.parse(url));
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
以下方法适用于所有隐式意图 - 不限于您关于浏览器的问题.
通常.当您发出隐含意图(如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);
}
Run Code Online (Sandbox Code Playgroud)