Open Chrome App with URL
Asked Answered
G

8

20

Is there a way to open Chrome app on Android from default Android browser? I'm able to open the app, but it doesn't redirect user to correct page. This is what I tried:

<a href="googlechrome://www.toovia.com">

I saw that I may have to form an intent URL, but I was hoping that there is a much easier way than that.

This is supposed to be from a web page and there is no web view involved.

Gerick answered 23/4, 2014 at 20:57 Comment(2)
Can this be done with a Javascript function or a script tag?Cahan
Here is a related question: #57912796Punt
M
18

Yes, but if it's not installed on the system you'll run into an ActivityNotFoundException. If it's not available, you should launch through the normal browser:

String url = "http://mysuperwebsite";
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 not installed
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(i);
}
Marcasite answered 23/4, 2014 at 21:30 Comment(4)
I actually need a URL since it's not an app. What does it look like then?Gerick
I can't help you with that, but having it as a normal URL should default to their selected browser or open up a chooser window (whichever they have selected) and is the normal process for Android.Marcasite
Hi, I want to load a html page in chrome browser, but it is not rendering intent.setData(Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/Test_4.html"));Kingcraft
How to do this with javascript ?Corrupt
S
14

Here is a solution without a try catch, if chrome is installed, it will be used. Otherwise, it will go to the device default

void open(Activity activity, String url) {
    Uri uri = Uri.parse("googlechrome://navigate?url=" + url);
    Intent i = new Intent(Intent.ACTION_VIEW, uri);
    if (i.resolveActivity(activity.getPackageManager()) == null) {
        i.setData(Uri.parse(url));
    }
    activity.startActivity(i);
}
Scorch answered 28/11, 2016 at 1:34 Comment(0)
K
4

The best thing is to detect the user browser

alert(navigator.userAgent);

and depending on a conditional statement

if (navigator.userAgent.indexOf('foo')) {
 // code logic
}

and based on that use BOM API to update window location

window.location.href = 'googlechrome://navigate?url='+ link;
Koosis answered 15/4, 2018 at 16:33 Comment(1)
But I had a problem with this in Android, so did others.... #57912796Eleanoreleanora
S
2

I opened my Safe Browser App with getting package name from Google play, the same way you can open for chrome by default also:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://Your URL"));
intent.setPackage("com.cloudacl");  // package of SafeBrowser App 
startActivity(intent);

You can replace package com.cloudacl with this com.android.chrome to open chrome.

Sherillsherilyn answered 29/1, 2016 at 10:2 Comment(0)
B
1

I am using below code to open Chrome browser from Android app

String urlString = "http://google.com";
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)
{
    //if Chrome browser not installed
    intent.setPackage(null);
    context.startActivity(intent);
}
Bigamy answered 17/12, 2019 at 13:11 Comment(0)
Z
1

Android Kotlin

Try this below code for intent to chrome browser in kotlin.

val uri = Uri.parse("googlechrome://navigate?url="+"https://stackoverflow.com/")
val i = Intent(Intent.ACTION_VIEW, uri)
if (i.resolveActivity([email protected]) == null) {
   i.data = Uri.parse("https://stackoverflow.com/")
}
[email protected](i)
Zolly answered 13/1, 2020 at 11:0 Comment(0)
H
0

It works for me :

try {
   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setData(Uri.parse(url));
   intent.setPackage("com.android.chrome");
   activity.startActivity(intent);
} catch (ActivityNotFoundException e) {
   // chrome app not install
}
Headstock answered 17/8, 2021 at 2:48 Comment(0)
R
-1

The Intent solutions above no longer seem to work.

The following works for me ..

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

Resent answered 15/8, 2016 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.