我目前正在测试一个使用大量jQuery动画开发的web应用程序,我们注意到内置web浏览器的性能非常差。在Chrome上测试时,web应用程序的性能快得令人难以置信。我只是想知道是否有任何类型的脚本可以强制在Chrome for Android中打开一个链接,就像在iOS中一样。
发布于 2015-01-05 21:02:35
实现这一点的一种更好的方法是照常使用Intent.ACTION_VIEW intent,但将com.android.chrome包添加到intent中。无论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设备:
以防您在Amazon Kindle中未安装chrome应用程序时打开Amazon默认浏览器
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);
}发布于 2012-08-18 06:01:58
有两种解决方案。
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);
}由方案提供的
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
}警告!以下技术在最新版本的上不起作用。它在这里作为参考,因为这个解决方案已经存在了一段时间:
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
}发布于 2015-12-28 18:29:01
所有提出的解决方案对我来说都不再有效。感谢@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);
}
}https://stackoverflow.com/questions/12013416
复制相似问题