80

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.

live-love
  • 48,840
  • 22
  • 240
  • 204
user1607943
  • 801
  • 1
  • 7
  • 3

12 Answers12

89

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);
}
Community
  • 1
  • 1
Martin
  • 1,777
  • 1
  • 14
  • 18
  • 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"); – markdwhite Nov 19 '15 at 07:11
  • @markdwhite In my kindle device I have both and this code opens chrome just **perfectly**. – Maveňツ Jan 09 '17 at 06:18
  • @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) – markdwhite Jan 09 '17 at 08:15
  • @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ňツ Jan 09 '17 at 09:48
  • @markdwhite SO works for both i.e OP and future readers like me :-) **#soReadyToHelp** – Maveňツ Jan 09 '17 at 11:00
  • What if **http** is not supplied in the url string? – IgorGanapolsky May 15 '17 at 18:08
  • @Martin - is this code javascript supported>?? i mean how can i start `Intent` from javascript??? – ddor254 Jun 05 '18 at 12:45
  • 2
    @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. – Martin Jun 05 '18 at 16:19
  • @martin - thank you for the quick feedback, i was 90% sure that this is possible , by now it seems like you are right. – ddor254 Jun 06 '18 at 07:44
  • context is showing error its showing to create variable, field or parameter – Karan Sethi Aug 04 '20 at 06:16
58

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
    }
Bonatti
  • 2,778
  • 5
  • 23
  • 42
philippe_b
  • 38,730
  • 7
  • 57
  • 59
  • 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? – user1607943 Aug 20 '12 at 20:21
  • 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... – philippe_b Aug 20 '12 at 21:12
  • Hi, thanks for this code. Is it possible to launch chrome with specific options? E.g. I want to launch it in fullscreen mode? – user826955 Oct 02 '13 at 12:48
  • 2
    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 – Someone Somewhere Jan 23 '14 at 23:23
  • 1
    this does not open chrome even when chrome is installed. it always goes to catch – Alireza Ahmadi Nov 15 '15 at 08:31
  • The solution stopped working for me. Had to search for another one http://stackoverflow.com/a/34491791/527759 – Eugene Popovich Dec 28 '15 at 10:42
  • @httpdispatch Thank you for the feedback. That's right, it doesn't work anymore for me either. I've just updated the answer. – philippe_b Dec 28 '15 at 22:08
  • @philippe_b the solution by package is not working at Android 5.1 – Eugene Popovich Dec 29 '15 at 06:41
  • @httpdispatch I've just run it an Android 6 device and it worked. What happens on your side? – philippe_b Dec 29 '15 at 09:17
  • @philippe_b for me it either launches default browser if specified or shows pick browser dialog if there are no default browser specified – Eugene Popovich Dec 29 '15 at 09:44
  • @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. – philippe_b Dec 29 '15 at 15:18
  • @philippe_b perhaps that is not because of chrome but because of Android OS itself – Eugene Popovich Dec 30 '15 at 08:14
  • @httpdispatch Sure, but I find this strange. The behavior of Intent.setPackage and folks could have changed from 5.0 to 5.1? – philippe_b Dec 31 '15 at 06:23
9

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);
    }
}
Eugene Popovich
  • 3,343
  • 2
  • 30
  • 33
9

This works in Firefox and Opera

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

emrekose26
  • 683
  • 1
  • 8
  • 21
Tim Smith
  • 682
  • 8
  • 5
4

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"));
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
Tushar Seth
  • 563
  • 7
  • 15
3

Here's the closest I've found: Writing a url and replacing http with googlechrome will open Chrome, but it doesn't seem to open the url specified. I'm working with a Samsung Galaxy S5, Android 5.0

That's the best I've found - every other solution I've seen on SO has required an Android app, not a webapp.

pixelbandito
  • 480
  • 6
  • 12
2

FOllowing up on @philippe_b's answer, I would like to add that this code will not work if Chrome is not installed. There is one more case in which it will not work - that is the case when Chrome is NOT selected as the default browser (but is installed) OR even if no browser is selected as the default.

In such cases, add the following catch part of the code also.

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

user1406716
  • 9,565
  • 22
  • 96
  • 151
2

Here's a more generic approach - it first find outs the package name for the default browser, which handles "http://" URLs, then uses the approach mentioned in the other answers to explicitly open the URL in a browser:

    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);
    }
Yaron Budowski
  • 420
  • 5
  • 15
2

Android open a link in chrome using Java :

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 open a link in chrome using Kotlin :

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)
}
Aftab Alam
  • 1,969
  • 17
  • 17
0

In google play there is a big variety of chrome browser apps with different features

So it's correct to check all of them

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()
    }
}
Vlad
  • 7,997
  • 3
  • 56
  • 43
0

The following worked for me, inspired from the documentation of 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);
}
Stephane JAIS
  • 1,413
  • 15
  • 14
0

It will open the link with chrome, if chrome is not installed in the application, it will open the default browser and while doing this, your application will not appear in the intent chooser.

override fun navigateAppToBrowser(url: String) {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    intent.setPackage("com.android.chrome")
    try {
        startActivity(intent)
    } catch (ex: Exception) {
        // Chrome browser presumably not installed so allow user to choose instead
        val defaultBrowser = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_BROWSER)
        defaultBrowser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        defaultBrowser.data = Uri.parse(url)
        if (defaultBrowser.resolveActivity(packageManager) != null) {
            startActivity(defaultBrowser)
        }
    }
}
Yasin Ege
  • 605
  • 4
  • 14