Is there any way in Android to force open a link to open in Chrome?

后端 未结 10 1138
情书的邮戳
情书的邮戳 2020-11-28 03:37

I\'m currently testing a webapp developed with lots of jQuery animations, and we\'ve noticed really poor performance with the built-in web browser. While testing in Chrome,

10条回答
  •  不知归路
    2020-11-28 04:20

    All the proposed solutions doesn't work for me anymore. Thanks to @pixelbandito, he pointed me to the right direction. I've found the next constant in the chrome sources

    public static final String GOOGLECHROME_NAVIGATE_PREFIX = "googlechrome://navigate?url=";
    

    And the next usage:

     Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("googlechrome://navigate?url=chrome-native://newtab/"));
    

    So the solution is (note the url should not be encoded)

    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);
        }
    }
    

提交回复
热议问题