我目前正在测试使用大量jQuery动画开发的webapp,我们注意到内置Web浏览器的性能非常差。在Chrome中进行测试时,Web应用程序的性能速度令人难以置信。我只是想知道是否有任何类型的脚本会强制在Chrome for Android中打开一个链接,类似于在iOS中完成的操作。
实现这一目标的更优雅的方法是正常使用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中没有安装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);
}
有两种解决方案。
通过包裹
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
}
所有提出的解决方案都不再适用于我。感谢@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/"));
所以解决方案是(注意不应该编码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);
}
}
这适用于Firefox和Opera
document.location = 'googlechrome://navigate?url=www.example.com/';
关于@ 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);
}
这是我发现的最接近的:写一个网址并用http
替换googlechrome
会打开Chrome,但它似乎没有打开指定的网址。我正在使用三星Galaxy S5,Android 5.0
这是我发现的最好的 - 我在SO上看到的所有其他解决方案都需要Android应用程序,而不是webapp。
在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()
}
}
上面的不同答案都很好但没有一个是完整的。这对我来说最适合我:
尝试打开chrome web浏览器,如果发生异常(chrome不是默认或未安装),将要求从用户选择浏览器:
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"));
}