有没有办法在浏览器窗口中打开链接?

问题描述 投票:0回答:2

我目前用的是这个

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

它当前在我的默认浏览器中打开。

但我需要它打开像这样

有什么简单的方法可以做到吗?

android android-studio
2个回答
1
投票

如果您想打开任何特定的浏览器应用程序,而不是在默认浏览器上打开网址。例如,如果您只想在 Chrome 应用程序上打开 URL,则将组件设置为您的隐式意图,那么您可以这样调用它:

String url = "https://www.google.com";
try {
  Intent i = new Intent("android.intent.action.MAIN");
  i.setComponent("com.android.chrome/com.android.chrome.Main");
  i.addCategory("android.intent.category.LAUNCHER");
  i.setData(Uri.parse(url));
  startActivity(i);
} catch(ActivityNotFoundException e) {
  // Google chrome application is not installed in user's phone
  Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  startActivity(i);
}

有时可能会发生应用程序在用户手机上不可用的情况,在这种情况下捕获异常并打开默认浏览器。


0
投票

您可以使用自定义选项卡来执行此操作: https://developer.chrome.com/docs/android/custom-tabs/guide-get-started

© www.soinside.com 2019 - 2024. All rights reserved.