use*_*943 58 browser mobile android google-chrome
我目前正在测试使用大量jQuery动画开发的webapp,我们注意到内置Web浏览器的性能非常差.在Chrome中进行测试时,Web应用程序的性能速度令人难以置信.我只是想知道是否有任何类型的脚本会强制在Chrome for Android中打开一个链接,类似于在iOS中完成的操作.
Mar*_*tin 66
实现此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);
}
Run Code Online (Sandbox Code Playgroud)
如果你想打开亚马逊默认浏览器以防万一在亚马逊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);
}
Run Code Online (Sandbox Code Playgroud)
phi*_*e_b 46
有两种解决方案.
通过包裹
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);
}
Run Code Online (Sandbox Code Playgroud)
按计划
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
}
Run Code Online (Sandbox Code Playgroud)
警告!以下技术不适用于最新版本的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
}
Run Code Online (Sandbox Code Playgroud)
所有提出的解决方案都不再适用于我.感谢@pixelbandito,他指出了正确的方向.我在铬源中找到了下一个常量
public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";
Run Code Online (Sandbox Code Playgroud)
接下来的用法:
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));
Run Code Online (Sandbox Code Playgroud)
所以解决方案是(注意不应该编码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);
}
}
Run Code Online (Sandbox Code Playgroud)