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

Viewed 118796

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.

11 Answers

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

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

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

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

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