我目前正在测试一个带有大量jQuery动画的Web应用程序,我们已经注意到内置Web浏览器的性能非常差。 在Chrome中进行测试时,Web应用程序的性能惊人地更快。 我只是想知道是否有任何类型的脚本可以强制在Android Chrome中打开链接,类似于在iOS中执行的方式。
我目前正在测试一个带有大量jQuery动画的Web应用程序,我们已经注意到内置Web浏览器的性能非常差。 在Chrome中进行测试时,Web应用程序的性能惊人地更快。 我只是想知道是否有任何类型的脚本可以强制在Android Chrome中打开链接,类似于在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);
}
intent.setPackage("com.amazon.cloud9");
- markdwhite有两个解决方案。
通过包
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
}
try{}
中 - 我刚在一部没有安装 Chrome 的手机上进行了测试,显式意图导致应用程序崩溃。 - Someone Somewhere所有提出的解决方案对我来说都不再起作用了。感谢@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/"));
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/';
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"));
}
private static final String TAG = "MainActivity";
- Favourite Videoshttp
替换为googlechrome
可以打开Chrome浏览器,但似乎无法打开指定的网址。我使用三星Galaxy S5,Android 5.0系统。这是我找到的最佳解决方案 - 我在SO上看到的其他解决方案都需要一个安卓应用程序,而不是一个Web应用程序。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);
}
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)
}
在@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);
}
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);
}
以下是我使用的方法,灵感来自CATEGORY_APP_BROWSER的文档:
Uri uri = Uri.parse("https://www.wonderpush.com");
Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER);
intent.setData(uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
Logging.loge("No activity for intent " + intent, e);
}