11

Hi I want to open the website in chrome app from my app webview when user click on particular link. I see this is possible https://developer.chrome.com/multidevice/android/intents, here on this it's for the zxing app, not for google chrome.

<a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"> Take a QR code </a>

I want same syntax for Google Chrome. Currently I am opening link in the webview, on click on link I want to specify a intent of chrome there like zxing.

In other words, I have a webview in which I opened a particular URL then after if user click on "XYZ" or something in the webview there then it should open google chrome. So for this i will add some chrome intent tag in the html that's syntax I am looking –

Please share if you know syntax for opening Google Chrome any hyperlink.

Thanks in advance.

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • 5
    *"I want to open the website in chrome app from my app when user click on particular link"* - what makes you so sure your users will have the Chrome app installed? – MH. Mar 25 '15 at 08:12
  • I second the above comment. And you are also not clear what you are looking for. Android Webview use webkit engine to render webpages, and chrome also uses the same. – Murtaza Khursheed Hussain Apr 16 '15 at 09:30

6 Answers6

48

It seems you're looking for the new Android Intents link that will create and explicit intent for the device for a link.

<a href="intent://<URL>#Intent;scheme=http;package=com.android.chrome;end">

works for me. so

<a href="intent://stackoverflow.com/questions/29250152/what-is-the-intent-to-launch-any-website-link-in-google-chrome#Intent;scheme=http;package=com.android.chrome;end"> 

will take you to this question in Chrome. Note that the scheme is specified separately so if you want to launch https links, you'd have to change scheme to scheme=https

But as everyone is saying, an explicit Chrome intent is a very non-Android thing to do. A better way would be to specify the ACTION_VIEW action like so:

<a href="intent://stackoverflow.com/questions/29250152/what-is-the-intent-to-launch-any-website-link-in-google-chrome#Intent;scheme=http;action=android.intent.action.VIEW;end;">

Source: The same page you linked

Thanks, I learned something today!

A J
  • 1,080
  • 7
  • 11
  • Works great for me when I stick my test page in a webview or view it via any browser. Once more, consider using the second way just in case the use doesn't have Chrome installed. – A J Apr 17 '15 at 15:45
  • 1
    Is there any way to do this via intent's I keep getting "Navigation is unreachable: intent:", source: .... which means its crashing after hitting the ampersand. Does it have to be in a web view? – Doug Ray Jun 22 '16 at 02:43
  • I don't really know what ampersand this is, but did you try escaping the ampersand? And your re:question about doing this via Intents, this is a way for browsers and webviews to create an Intent. You can just make a normal ACTION_VIEW Intent if you're not using a browser like `new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));` – A J Jun 22 '16 at 23:48
  • 1
    what if he doesn't have chrome installed? he will be redirected to play store? – the_nuts Jul 24 '18 at 16:05
  • @the_nuts no, which is why the second intent is better. To redirect to play store you'd probably need server side logic to route appropriately. – A J Dec 09 '19 at 17:51
  • 1
    this works great on mobile, but on desktop it does nothing, even when the fallback URL is specified, any idea? – shamaseen Aug 10 '21 at 12:21
  • @shamaseen you can try [dynamic links](https://firebase.google.com/docs/dynamic-links) – wowandy Sep 15 '21 at 11:46
8

Below code make launch urls in webview including intent:// url scheme in Android.

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url != null) {
        if (url.startsWith("http://") || url.startsWith("https://")) {
            view.loadUrl(url);
        } else if (url.startsWith("intent://")) {
            try {
                Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                Intent existPackage = getPackageManager().getLaunchIntentForPackage(intent.getPackage());
                if (existPackage != null) {
                    startActivity(intent);
                } else {
                    Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                    marketIntent.setData(Uri.parse("market://details?id=" + intent.getPackage()));
                    startActivity(marketIntent);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (url.startsWith("market://")) {
            try {
                Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                if (intent != null) {
                    startActivity(intent);
                }
                return true;
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
        } else { // unhandled url scheme
             view.getContext().startActivity(
                    new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
        return true;
    } else {
        return false;
    }
}
Youngjae
  • 24,352
  • 18
  • 113
  • 198
3

I have tested below code with Nexus 6 with Chrome and Mozilaa installed and it works great,

    String url = "http://www.stackoverflow.com";
    Intent i = new Intent();
    i.setPackage("com.android.chrome");
    i.setAction(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);

This will give error if Chrome is not installed in your device. So put check for package availability.

MKJParekh
  • 34,073
  • 11
  • 87
  • 98
2

Use this:

String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

hope this will help..

Avishek Das
  • 661
  • 1
  • 6
  • 20
  • 1
    Add `i.setPackage("com.android.chrome");` to make sure it is launched only with Chrome (otherwise, it will launch with any web browser installed on the device). It is usually recommended NOT to specify the package since you can't be sure that the required app is installed, but if you choose to do that, at least wrap it with `try...catch` block to handle `ActivityNotFound` exception – Muzikant Apr 13 '15 at 14:35
1

This code is to open an android application from your chrome browser. You can check this from this link

  <a href="intent://scan/#Intent;scheme=zxing;package=com.google.zxing.client.android;end"> Take a QR code </a>

I am having another way of opening Chrome browser from your application

private class MyWebViewClient extends WebViewClient {

public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {

   String url = Uri.parse(paramString);
   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);
   }
  }
}
bGorle
  • 1,978
  • 2
  • 22
  • 28
0

Google Chrome app Package name is com.android.chrome so ready your Intent URL using below concept

     intent:
    //scan/
    #Intent; 
    package=com.android.chrome; 
    scheme=zxing; 
    end; 
KDOSHI
  • 309
  • 4
  • 15
  • where to specify the link ? – N Sharma Mar 25 '15 at 08:58
  • In textview or button or any link where ever you want ;just define the Onclick method and pass the Intent using this Link. – KDOSHI Mar 25 '15 at 09:58
  • can we specify a hyperlink in the intent ? – N Sharma Mar 25 '15 at 12:26
  • You have to find out the specific Put Extra key for the Specific Intent than pass that value specifically. – KDOSHI Mar 25 '15 at 13:03
  • I'm not looking which you are talking. I have a webview in which I opened a particular URL then after if user click on "XYZ" or something in the webview there then it should open google chrome. So for this i will add some chrome intent tag in the html that's syntax I am looking – N Sharma Apr 11 '15 at 12:28
  • change `scheme` to `http` and add `data=YOUR_URL;` – Muzikant Apr 13 '15 at 14:38