18

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.

Xan
  • 66,873
  • 13
  • 150
  • 174
juminoz
  • 3,240
  • 7
  • 32
  • 48
  • 1
    Can this be done with a Javascript function or a script tag? – Derek Apr 19 '18 at 18:03
  • Here is a related question: https://stackoverflow.com/questions/57912296/not-allowed-to-load-local-resource-trying-opening-googlechrome-navigateurl-xx – Juan Rojas Jul 17 '20 at 06:28

8 Answers8

15

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);
}
Cruceo
  • 6,574
  • 2
  • 27
  • 48
  • 2
    I actually need a URL since it's not an app. What does it look like then? – juminoz Apr 24 '14 at 01:33
  • 1
    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. – Cruceo Apr 25 '14 at 16:17
  • 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")); – kashyap Jan 25 '19 at 06:53
  • How to do this with javascript ? – yestema Dec 09 '19 at 19:12
11

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);
}
Ilya Gazman
  • 27,805
  • 19
  • 119
  • 190
3

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;
  • But I had a problem with this in Android, so did others.... https://stackoverflow.com/questions/57912296/not-allowed-to-load-local-resource-trying-opening-googlechrome-navigateurl-xx – lin Jan 20 '21 at 08:40
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.

Vasily Kabunov
  • 5,179
  • 12
  • 41
  • 46
karan
  • 3,150
  • 1
  • 32
  • 44
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);
}
R15
  • 9,066
  • 8
  • 50
  • 105
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(this@MainActivity.packageManager) == null) {
   i.data = Uri.parse("https://stackoverflow.com/")
}
this@MainActivity.startActivity(i)
Aftab Alam
  • 1,193
  • 9
  • 12
0

Your xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

Your WebViewActivity:

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.google.com");
    }
}
Vasily Kabunov
  • 5,179
  • 12
  • 41
  • 46
josedlujan
  • 4,629
  • 2
  • 24
  • 42
  • Again this doesn't answer my question. This is not for an app. It's opening an app from browser. I have no access to web view. – juminoz Apr 24 '14 at 15:15
-1

The Intent solutions above no longer seem to work.

The following works for me ..

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

Tim Smith
  • 332
  • 4
  • 4