Android 中是否有任何方法可以强制打开要在 Chrome 中打开的链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12013416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there any way in Android to force open a link to open in Chrome?
提问by user1607943
I'm currently testing a webapp developed with lots of jQuery animations, and we've noticed really poor performance with the built-in web browser. While testing in Chrome, the performance of the webapp is unbelievably quicker. I'm just wondering if there was any type of script that would force open a link in Chrome for Android, similar to how it's done in iOS.
我目前正在测试使用大量 jQuery 动画开发的 web 应用程序,我们注意到内置 web 浏览器的性能非常差。在 Chrome 中进行测试时,webapp 的性能快得令人难以置信。我只是想知道是否有任何类型的脚本可以在 Chrome for Android 中强制打开链接,类似于在 iOS 中的做法。
回答by Martin
A more elegant way to achieve this is to use the Intent.ACTION_VIEW
intent as normal, but add the package com.android.chrome
to the intent. This works regardless of whether Chrome is the default browser and ensures exactly the same behavior as if the user had selected Chrome from the chooser list.
实现此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);
}
Update
更新
For Kindle Devices:
对于 Kindle 设备:
Just in case if you want to open Amazon Default Browser in case chrome app is not installed in Amazon 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);
}
回答by philippe_b
There are two solutions.
有两种解决方案。
By package
按包
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);
}
By scheme
按方案
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
}
WARNING!The following technique does not workon most recent versions of Android. It is here for reference, because this solution has been around for a while:
警告!以下技术不适用于最新版本的 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
}
回答by httpdispatch
All the proposed solutions doesn't work for me anymore. Thanks to @pixelbandito, he pointed me to the right direction. I've found the next constant in the chrome sources
所有提出的解决方案都不再适合我。感谢@pixelbandito,他为我指明了正确的方向。我在 chrome 源代码中找到了下一个常量
public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";
And the next usage:
和下一个用法:
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));
So the solution is (note the url should not be encoded)
所以解决方案是(注意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);
}
}
回答by Tim Smith
This works in Firefoxand Opera
这适用于Firefox和Opera
document.location = 'googlechrome://navigate?url=www.example.com/';
document.location = 'googlechrome://navigate?url=www.example.com/';
回答by user1406716
FOllowing up on @philippe_b's answer, I would like to add that this code will not work if Chrome is not installed. There is one more case in which it will not work - that is the case when Chrome is NOT selected as the default browser (but is installed) OR even if no browser is selected as the default.
继@philippe_b 的回答之后,我想补充一点,如果未安装 Chrome,此代码将不起作用。还有一种情况下它不起作用 - 当 Chrome 未被选为默认浏览器(但已安装)或即使没有选择浏览器作为默认浏览器时也会出现这种情况。
In such cases, add the following catch part of the code also.
在这种情况下,还要添加以下代码的 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);
}
回答by pixelbandito
Here's the closest I've found: Writing a url and replacing http
with googlechrome
will open Chrome, but it doesn't seem to open the url specified. I'm working with a Samsung Galaxy S5, Android 5.0
这是我找到的最接近的:编写一个 url 并替换http
为googlechrome
将打开 Chrome,但它似乎没有打开指定的 url。我正在使用三星 Galaxy S5、Android 5.0
That's the best I've found - every other solution I've seen on SO has required an Android app, not a webapp.
这是我找到的最好的 - 我在 SO 上看到的所有其他解决方案都需要一个 Android 应用程序,而不是一个 web 应用程序。
回答by Tushar Seth
The different answers above are good but none is complete. This in all suited me the best which will :
上面的不同答案很好,但没有一个是完整的。这一切最适合我,它将:
try to open chrome web browser and in case exception occurs(chrome is not default or not installed), will ask for choosing the browser from user:
尝试打开 chrome web 浏览器,如果发生异常(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"));
}
回答by Yaron Budowski
Here's a more generic approach - it first find outs the package name for the default browser, which handles "http://" URLs, then uses the approach mentioned in the other answers to explicitly open the URL in a browser:
这是一种更通用的方法 - 它首先找出默认浏览器的包名称,它处理“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);
}
回答by Aftab Alam
Android open a link in chrome using Java :
Android 在 chrome 中使用 Java 打开一个链接:
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 open a link in chrome using Kotlin :
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)
}
回答by Vlad
In google play there is a big variety of chrome browser apps with different features
在 google play 中有各种各样具有不同功能的 chrome 浏览器应用程序
So it's correct to check all of them
所以检查所有这些是正确的
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()
}
}