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);
}
Answer from Cruceo on Stack OverflowBefore you ask yes I did change chrome to be my default browser app! I just got this phone and I don't know what to do to fix this... and googling isn't helping me because it's just either saying change the default app which I did or turn off in app browser all together which I don't want. Please help thanks. 🥺🥺
https://imgur.com/s9AMu4X
Videos
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);
}
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);
}
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);
}
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
}
So I tried to solve this on my own and I give up haha. You know when you click a link for Instagram/Facebook/Twitter/Reddit on Google Chrome and it'll ask something like "open with Chrome or Facebook" and you can choose whether it opens in the browser or in the app if you have it downloaded? And then you can say to use that option always or just once. Well I always say just once because I may want either option depending on the situation, but today I accidentally hit always for opening a Facebook link in Chrome instead of in app. Now I can't click a link and tell Chrome to take me to the Facebook app. And I can't figure out how to change the setting to preferably ask me everytime or at the very least always open in the app.
All my Google searching is leading me to settings->apps->default apps->opening links->facebook and setting open supported links to always ask. But it's already set to that and it does nothing, I believe that's because it's referring to when I'm in the Facebook app already and click a link, do I want it to open in the app or in a browser.
So now I'm stuck. I restarted my phone and deleted and reinstalled Facebook. Nothin. Sorry for the wall of text for seemingly a simple issue.
tldr: (Pixel 5, latest Android update) I selected always open with Google Chrome when clicking a Facebook link in Chrome, can't find setting to make it ask again or change it to always open in Facebook app.
For Android:
<a href="intent:https://yourwebsite.com#Intent;end" target="_blank">Open Browser</a>
Note: This solution will open default browser (if its set, if no default browser set, it will show list of browsers which supports opening url)
Make sure that, the click is generated by user (Not Javascript) to test this. Some browsers wont open if its NOT called by user.
I was looking for the answer too as I've tried to send my users from unsupported browsers (for ex: Facebook browser) to chrome.
Unfortunately, John's solution didn't work for me but it pointed in the right direction.
What works for me:
<a href="intent://mysite.com#Intent;scheme=https;package=com.android.chrome;end">
Click to open on Chrome
</a>
Explanation
I've found a good explanation at https://branch.io/glossary/chrome-intents/ , but to sum it up:
Split it to 3 vars:
URI: the name of the site (ex: google.com), and notice its without the UriSchemes.
UriSchemes: HTTP / https (you can read more about it at https://www.w3.org/wiki/UriSchemes)
PACKAGE: the package of the app we want to send the intent to, in our case its com.android.chrome
now you can just insert them in the code below
<a href="intent://URI#Intent;scheme=UriSchemes;package=PACKAGE">
Click to open on Chrome
</a>