Open Chrome App with URL

我与影子孤独终老i 提交于 2021-02-05 20:23:56

问题


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.


回答1:


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);
}



回答2:


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);
}



回答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;



回答4:


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.




回答5:


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);
}



回答6:


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)



回答7:


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");
    }
}



回答8:


The Intent solutions above no longer seem to work.

The following works for me ..

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



来源:https://stackoverflow.com/questions/23255026/open-chrome-app-with-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!