user1607943
user1607943

Reputation: 851

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

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.

Upvotes: 85

Views: 137512

Answers (12)

Yasin Ege
Yasin Ege

Reputation: 723

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

Upvotes: 0

Stephane JAIS
Stephane JAIS

Reputation: 1433

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

Upvotes: 0

Aftab Alam
Aftab Alam

Reputation: 2049

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

Upvotes: 3

Tushar Seth
Tushar Seth

Reputation: 613

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

Upvotes: 4

Yaron Budowski
Yaron Budowski

Reputation: 439

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

Upvotes: 2

Vlad
Vlad

Reputation: 8553

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

Upvotes: 0

Martin
Martin

Reputation: 1807

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

Upvotes: 90

pixelbandito
pixelbandito

Reputation: 555

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.

Upvotes: 3

Tim Smith
Tim Smith

Reputation: 682

This works in Firefox and Opera

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

Upvotes: 9

philippe_b
philippe_b

Reputation: 41318

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
    }

Upvotes: 58

Eugene Popovich
Eugene Popovich

Reputation: 3473

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

Upvotes: 9

user1406716
user1406716

Reputation: 9705

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

Upvotes: 2

Related Questions