Android中有没有办法强制打开链接在Chrome中打开?

我目前正在testing一个用很多Jqueryanimation开发的webapp,而且我们注意到内置web浏览器的性能非常差。 在Chrome中进行testing时,webapp的性能令人难以置信地更快。 我只是想知道是否有任何types的脚本,将强制在Android版Chrome中打开链接,类似于iOS中的操作。

一个更好的方法来实现这一点,就像正常使用Intent.ACTION_VIEW意图一样,但是将com.android.chrome包添加到intent中。 无论Chrome是否为默认浏览器,此function都能正常工作,并确保与用户从select器列表中selectChrome完全相同的行为。

 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设备:

以防万一,如果你想打开亚马逊默认浏览器的情况下chrome应用程序未安装在亚马逊的Kindle

 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); } 

有两个解决scheme。

通过包装

  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。 这里仅供参考,因为这个解决scheme已经有一段时间了:

  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 } 

所有build议的解决scheme不再适用于我。 感谢@pixelbandito,他指出我正确的方向。 我发现铬源中的下一个常量

 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/")); 

所以解决scheme是(注意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); } } 

FOllowing @ philippe_b的答案,我想补充说,如果没有安装Chrome,这段代码将不起作用。 还有一种情况是不起作用的,就是在没有selectChrome作为默认浏览器(但已安装)的情况下,或者即使没有select浏览器作为默认浏览器。

在这种情况下,还要添加以下代码的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); } 

这适用于FirefoxOpera

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

下面是我find的最近的:写一个url并用googlechromereplacehttp将打开Chrome,但似乎并没有打开指定的URL。 我正在使用三星Galaxy S5,Android 5.0

这是我发现的最好的 – 我见过的其他所有解决scheme都需要Android应用程序,而不是Web应用程序。