Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Chrome App with URL

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.

like image 393
juminoz Avatar asked Apr 23 '14 20:04

juminoz


People also ask

How do I open a Chrome app window?

Click the Chrome menu button ( ⋮ or ☰ ) Select More tools → Create shortcut... Go to chrome://apps and find your new shortcut. Right-click on icon, and select “Open as window”


3 Answers

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);
}
like image 106
Cruceo Avatar answered Sep 19 '22 12:09

Cruceo


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);
}
like image 27
Ilya Gazman Avatar answered Sep 18 '22 12:09

Ilya Gazman


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;
like image 34
Maen K Househ Avatar answered Sep 19 '22 12:09

Maen K Househ