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

114,402

Solution 1

A more elegant way to achieve this is to use the Intent.ACTION_VIEW intent as normal, but add the package com.android.chrome to the intent. This works regardless of whether Chrome is the default browser and ensures exactly the same behavior as if the user had selected Chrome from the chooser list.

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

Update

For Kindle Devices:

Just in case if you want to open Amazon Default Browser in case chrome app is not installed in Amazon Kindle

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

Solution 2

There are two solutions.

By package

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

By scheme

    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
    }

WARNING! The following technique does not work on most recent versions of Android. It is here for reference, because this solution has been around for a while:

    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
    }

Solution 3

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

Solution 4

This works in Firefox and Opera

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

Solution 5

The different answers above are good but none is complete. This in all suited me the best which will :

try to open chrome web browser and in case exception occurs(chrome is not default or not installed), will ask for choosing the browser from user:

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"));
}
Share:
114,402
user1607943
Author by

user1607943

Updated on July 05, 2022

Comments

  • user1607943
    user1607943 almost 2 years

    I'm currently testing a web app developed with lots of jQuery animations, and we've noticed really poor performance with the built-in web browser. While testing in Chrome, the performance of the web app is unbelievably quicker. I'm just wondering if there was any type of script that would force open a link in Chrome for Android, similar to how it's done in iOS.

    • Cat
      Cat over 11 years
      What if Chrome isn't installed on the device?
    • Maveňツ
      Maveňツ over 7 years
      @Eric you may would like to check this
  • user1607943
    user1607943 over 11 years
    phillippe_b, thanks for the code. Any suggestions where to place this code? Some of the syntax is unfamiliar to me. Is this meant to be placed in eclipse?
  • philippe_b
    philippe_b over 11 years
    This is some Java code, supposed to appear in an Android native application. This application can solely start Chrome. Although this is very simple, I admit it is a bit tedious if you know absolutely nothing about Android apps: how to code them, how to deploy them...
  • user826955
    user826955 over 10 years
    Hi, thanks for this code. Is it possible to launch chrome with specific options? E.g. I want to launch it in fullscreen mode?
  • Someone Somewhere
    Someone Somewhere over 10 years
    be sure to wrap that with a try{} - I just tested on a phone that didnt have chrome installed and the explicit intent crashed the app
  • Alireza Ahmadi
    Alireza Ahmadi over 8 years
    this does not open chrome even when chrome is installed. it always goes to catch
  • markdwhite
    markdwhite over 8 years
    FWIW, to do this on a Kindle and force Silk to open (rather than the user getting Shop Amazon as an option), use: intent.setPackage("com.amazon.cloud9");
  • Eugene Popovich
    Eugene Popovich over 8 years
    Thank you for the right direction. See my answer based on your found stackoverflow.com/a/34491791/527759
  • Eugene Popovich
    Eugene Popovich over 8 years
    The solution stopped working for me. Had to search for another one stackoverflow.com/a/34491791/527759
  • philippe_b
    philippe_b over 8 years
    @httpdispatch Thank you for the feedback. That's right, it doesn't work anymore for me either. I've just updated the answer.
  • Eugene Popovich
    Eugene Popovich over 8 years
    @philippe_b the solution by package is not working at Android 5.1
  • philippe_b
    philippe_b over 8 years
    @httpdispatch I've just run it an Android 6 device and it worked. What happens on your side?
  • Eugene Popovich
    Eugene Popovich over 8 years
    @philippe_b for me it either launches default browser if specified or shows pick browser dialog if there are no default browser specified
  • philippe_b
    philippe_b over 8 years
    @httpdispatch Humm :( It's strange that this feature suddenly stops working. Do you have any idea why it would fail? An hypothesis: Android Chrome package name changed recently. I tried to get the code to check this, but... it fails on my laptop. It's definitely not as easy as a git checkout.
  • Eugene Popovich
    Eugene Popovich over 8 years
    @philippe_b perhaps that is not because of chrome but because of Android OS itself
  • philippe_b
    philippe_b over 8 years
    @httpdispatch Sure, but I find this strange. The behavior of Intent.setPackage and folks could have changed from 5.0 to 5.1?
  • Maveňツ
    Maveňツ over 7 years
    @markdwhite In my kindle device I have both and this code opens chrome just perfectly.
  • markdwhite
    markdwhite over 7 years
    @MaňishYadav - you missed the point. If Chrome is not installed and if the desired action is to force the Silk browser to open, use my suggestion (which was added in case anyone ends up here needing to open Silk where Chrome is absent)
  • Maveňツ
    Maveňツ over 7 years
    @markdwhite If there is one browser in any type of device whether it is android or kindle this code will open that browser.. what is the issue with that? Obviously task given to that intent should be accomplished.
  • Maveňツ
    Maveňツ over 7 years
    @markdwhite SO works for both i.e OP and future readers like me :-) #soReadyToHelp
  • IgorGanapolsky
    IgorGanapolsky almost 7 years
    What if http is not supplied in the url string?
  • ddor254
    ddor254 almost 6 years
    @Martin - is this code javascript supported>?? i mean how can i start Intent from javascript???
  • Martin
    Martin almost 6 years
    @ddor254 this isn't javascript. By the sound of it you have a link in a web page and you want to force it to open in Chrome even if the user is viewing the web page in a different browser. That is not possible.
  • ddor254
    ddor254 almost 6 years
    @martin - thank you for the quick feedback, i was 90% sure that this is possible , by now it seems like you are right.
  • Favourite Videos
    Favourite Videos almost 5 years
    If you get error about the TAG constant, add this code to the activity. private static final String TAG = "MainActivity";
  • Karan Sethi
    Karan Sethi over 3 years
    context is showing error its showing to create variable, field or parameter