Android中是否有任何方法强制打开在Chrome中打开的链接?

时间:2012-08-17 21:29:25

标签: android google-chrome mobile browser

我目前正在测试使用大量jQuery动画开发的webapp,我们注意到内置Web浏览器的性能非常差。在Chrome中进行测试时,Web应用程序的性能速度令人难以置信。我只是想知道是否有任何类型的脚本会强制在Chrome for Android中打开一个链接,类似于在iOS中完成的操作。

10 个答案:

答案 0 :(得分:62)

更优雅的方法是正常使用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设备:

以防万一你想打开亚马逊默认浏览器以防万一在亚马逊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);
}

答案 1 :(得分:44)

有两种解决方案。

按包

    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
    }

答案 2 :(得分:8)

所有提议的解决方案都不再适用于我。感谢@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);
    }
}

答案 3 :(得分:6)

适用于 Firefox Opera

document.location = 'googlechrome://navigate?url=www.example.com/';

答案 4 :(得分:2)

了解@ 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);
}

答案 5 :(得分:2)

以下是我发现的最接近的内容:写一个网址并将http替换为googlechrome会打开Chrome,但它似乎没有打开指定的网址。我正在使用三星Galaxy S5,Android 5.0

这是我发现的最好的 - 我在SO上看到的所有其他解决方案都需要Android应用,而不是网络应用。

答案 6 :(得分:1)

Android使用Java在Chrome中打开链接:

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使用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)
}

答案 7 :(得分:0)

在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()
    }
}

答案 8 :(得分:0)

以上不同的答案都不错,但没有一个是完整的。总而言之,这最适合我:

尝试打开chrome网络浏览器,如果发生异常(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"));
                }

答案 9 :(得分:0)

这是一种更通用的方法-它首先找出默认浏览器的程序包名称,该默认浏览器处理“ http://” URL,然后使用其他答案中提到的方法在浏览器中显式打开URL:< / p>

response.debugDescription
相关问题