使用网址打开Chrome应用

jum*_*noz 9 html android google-chrome android-browser

有没有办法在默认Android浏览器上打开Android上的Chrome应用?我可以打开应用程序,但它不会将用户重定向到正确的页面.这是我试过的:

<a href="googlechrome://www.toovia.com">
Run Code Online (Sandbox Code Playgroud)

我看到我可能必须形成一个意图URL,但我希望有一个比这更简单的方法.

这应该来自网页,并且不涉及Web视图.

Cru*_*ceo 11

是的,但是如果它没有安装在系统上,你将遇到ActivityNotFoundException.如果它不可用,您应该通过普通浏览器启动:

String url = "http://mysuperwebsite";
try {
    Intent i = new Intent("android.intent.action.MAIN");
    i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main"));
    i.addCategory("android.intent.category.LAUNCHER");
    i.setData(Uri.parse(url));
    startActivity(i);
}
catch(ActivityNotFoundException e) {
    // Chrome is not installed
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)

  • 我实际上需要一个 URL,因为它不是一个应用程序。那它看起来像什么? (2认同)

Ily*_*man 9

这是一个没有try catch的解决方案,如果安装了chrome,它将被使用,否则它将转到设备默认

void open(Activity activity, String url) {
    Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
    Intent i = new Intent(Intent.ACTION_VIEW, uri);
    if (i.resolveActivity(activity.getPackageManager()) == null) {
        i.setData(Uri.parse(url));
    }
    activity.startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)