【问题标题】:Is there any way in Android to force open a link to open in Chrome?Android中有什么方法可以强制打开链接以在Chrome中打开?
【发布时间】:2012-08-14 08:27:09
【问题描述】:

我目前正在测试一个使用大量 jQuery 动画开发的网络应用程序,我们注意到内置网络浏览器的性能非常差。在 Chrome 中进行测试时,Web 应用程序的性能快得令人难以置信。我只是想知道是否有任何类型的脚本会强制在 Chrome for Android 中打开链接,类似于它在 iOS 中的完成方式。

【问题讨论】:

  • 如果设备上没有安装 Chrome 怎么办?
  • @Eric 你可能想查看this

标签: android google-chrome mobile browser


【解决方案1】:

Android 使用 Java 在 chrome 中打开链接:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("your url link"));
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setPackage("com.android.chrome");
try {
    context.startActivity(i);
} catch (ActivityNotFoundException e) {
 Toast.makeText(context, "unable to open chrome", Toast.LENGTH_SHORT).show();
 i.setPackage(null);
 context.startActivity(i);
}

Android 使用 Kotlin 在 chrome 中打开链接:

val i = Intent(Intent.ACTION_VIEW, Uri.parse("https://stackoverflow.com/"))
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
i.setPackage("com.android.chrome")
try {
  context!!.startActivity(i)
} catch (e: ActivityNotFoundException) {
  Toast.makeText(context, "unable to open chrome", Toast.LENGTH_SHORT).show()
  i.setPackage(null)
  context!!.startActivity(i)
}

【讨论】:

    【解决方案2】:

    上面的不同答案都很好,但没有一个是完整的。这最适合我:

    尝试打开chrome浏览器,如果出现异常(chrome不是默认或未安装),会要求用户选择浏览器:

    String uriString = "your uri string";
    
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriString));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setPackage("com.android.chrome");
    try {
        Log.d(TAG, "onClick: inTryBrowser");
        startActivity(intent);
    } catch (ActivityNotFoundException ex) {
        Log.e(TAG, "onClick: in inCatchBrowser", ex );
        intent.setPackage(null);
        startActivity(Intent.createChooser(intent, "Select Browser"));
    }
    

    【讨论】:

    • 如果您收到关于 TAG 常量的错误,请将此代码添加到活动中。 private static final String TAG = "MainActivity";
    【解决方案3】:

    这是一种更通用的方法 - 它首先找出默认浏览器的包名称,它处理“http://” URL,然后使用其他答案中提到的方法在浏览器中显式打开 URL:

        public void openUrlInBrowser(Context context, String url) {
            // Find out package name of default browser
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
            ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
            String packageName = resolveInfo.activityInfo.packageName;
    
            // Use the explicit browser package name
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            i.setPackage(packageName);
            context.startActivity(i);
        }
    

    【讨论】:

      【解决方案4】:

      在 google play 中有各种各样的 chrome 浏览器应用程序具有不同的功能

      所以检查所有这些是正确的

      fun Context.openChrome(url: String, onError: (() -> Unit)? = null) {
          openBrowser("com.android.chrome", url) {
              openBrowser("com.android.beta", url) {
                  openBrowser("com.android.dev", url) {
                      openBrowser("com.android.canary", url) {
                          onError?.invoke() ?: openBrowser(null, url)
                      }
                  }
              }
          }
      }
      
      fun Context.openBrowser(packageName: String?, url: String, onError: (() -> Unit)? = null) {
          try {
              startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)).apply {
                  setPackage(packageName)
                  addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
              })
          } catch (e: ActivityNotFoundException) {
              onError?.invoke()
          }
      }
      

      【讨论】:

        【解决方案5】:

        实现这一点的更优雅的方法是照常使用Intent.ACTION_VIEW 意图,但将包com.android.chrome 添加到意图中。无论 Chrome 是否是默认浏览器,这都有效,并确保与用户从选择器列表中选择 Chrome 时完全相同的行为。

        String urlString = "http://mysuperwebsite";
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setPackage("com.android.chrome");
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException ex) {
            // Chrome browser presumably not installed so allow user to choose instead
            intent.setPackage(null);
            context.startActivity(intent);
        }
        

        更新

        对于 Kindle 设备:

        以防万一你想打开亚马逊默认浏览器,以防亚马逊 Kindle 中没有安装 chrome 应用

        String urlString = "http://mysuperwebsite";
        Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(urlString));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setPackage("com.android.chrome");
        try {
            context.startActivity(intent);
        } catch (ActivityNotFoundException ex) {
            // Chrome browser presumably not installed and open Kindle Browser
            intent.setPackage("com.amazon.cloud9");
            context.startActivity(intent);
        }
        

        【讨论】:

        • FWIW,要在 Kindle 上执行此操作并强制 Silk 打开(而不是让用户选择 Shop Amazon),请使用:intent.setPackage("com.amazon.cloud9");
        • @markdwhite 在我的 Kindle 设备中我两者都有,而且这段代码完美地打开了 chrome
        • @MaňishYadav - 你没抓住重点。如果没有安装 Chrome,并且想要强制打开 Silk 浏览器,请使用我的建议(如果有人最终需要在 Chrome 不存在的情况下打开 Silk)
        • @markdwhite 如果任何类型的设备中都有一个浏览器,无论是 android 还是 kindle,此代码都会打开该浏览器。这有什么问题?显然应该完成分配给intent 的任务。
        • @ddor254 这不是 javascript。通过它的声音,您在网页中有一个链接,并且您想强制它在 Chrome 中打开,即使用户正在其他浏览器中查看该网页。这是不可能的。
        【解决方案6】:

        这是我找到的最接近的:编写一个 url 并用 googlechrome 替换 http 将打开 Chrome,但它似乎没有打开指定的 url。我使用的是三星 Galaxy S5,Android 5.0

        这是我发现的最好的 - 我在 SO 上看到的所有其他解决方案都需要 Android 应用,而不是 web 应用。

        【讨论】:

        【解决方案7】:

        这适用于 FirefoxOpera

        document.location = 'googlechrome://navigate?url=www.example.com/';

        【讨论】:

          【解决方案8】:

          有两种解决方案。

          按包

              String url = "http://www.example.com";
              Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              i.setPackage("com.android.chrome");
              try {
                  startActivity(i);
              } catch (ActivityNotFoundException e) {
                  // Chrome is probably not installed
                  // Try with the default browser
                  i.setPackage(null);
                  startActivity(i);
              }
          

          按方案

              String url = "http://www.example.com";
              try {
                  Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
                  Intent i = new Intent(Intent.ACTION_VIEW, uri);
                  i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivity(i);
              } catch (ActivityNotFoundException e) {
                  // Chrome is probably not installed
              }
          

          警告!以下技术在最新版本的 Android 上不起作用。在这里供参考,因为这个解决方案已经存在了一段时间:

              String url = "http://www.example.com";
              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 probably not installed
              }
          

          【讨论】:

          • phillippe_b,感谢您的代码。有什么建议在哪里放置此代码?有些语法我不熟悉。这是要放在eclipse中的吗?
          • 这是一些 Java 代码,应该出现在 Android 原生应用程序中。此应用程序可以单独启动 Chrome。虽然这很简单,但我承认如果你对 Android 应用程序一无所知,那就有点乏味了:如何编写代码,如何部署它们……
          • 您好,感谢您提供此代码。是否可以使用特定选项启动 chrome?例如。我想以全屏模式启动它?
          • 一定要用try{} 包装它 - 我刚刚在没有安装 chrome 的手机上进行了测试,并且显式意图使应用程序崩溃
          • 即使安装了 chrome,也不会打开 chrome。它总是赶上
          【解决方案9】:

          所有建议的解决方案都不再适合我了。感谢@pixelbandito,他为我指明了正确的方向。我在 chrome 源代码中找到了下一个常量

          public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";
          

          以及下一个用法:

           Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));
          

          所以解决方法是(注意url不应该编码)

          void openUrlInChrome(String url) {
              try {
                  try {
                      Uri uri = Uri.parse("googlechrome://navigate?url="+ url);
                      Intent i = new Intent(Intent.ACTION_VIEW, uri);
                      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      startActivity(i);
                  } catch (ActivityNotFoundException e) {
                      Uri uri = Uri.parse(url);
                      // Chrome is probably not installed
                      // OR not selected as default browser OR if no Browser is selected as default browser
                      Intent i = new Intent(Intent.ACTION_VIEW, uri);
                      i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      startActivity(i);
                  }
              } catch (Exception ex) {
                  Timber.e(ex, null);
              }
          }
          

          【讨论】:

            【解决方案10】:

            根据@philippe_b 的回答,我想补充一点,如果未安装 Chrome,此代码将不起作用。还有另一种情况下它不起作用 - 即没有选择 Chrome 作为默认浏览器(但已安装)或者即使没有选择浏览器作为默认浏览器。

            在这种情况下,还要在代码中添加以下 catch 部分。

            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("http://mysuperwebsite"));
                startActivity(i);
            }
            catch(ActivityNotFoundException e) {
            // Chrome is probably not installed 
            // OR not selected as default browser OR if no Browser is selected as default browser
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("somesite.com"));
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(i);
            }
            

            【讨论】:

              猜你喜欢
              • 2018-07-29
              • 1970-01-01
              • 2020-08-15
              • 2020-03-10
              • 2021-12-22
              • 1970-01-01
              • 2011-08-18
              相关资源
              最近更新 更多