Android 使用 URL 打开 Chrome 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23255026/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Chrome App with URL
提问by juminoz
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:
有没有办法从默认的 Android 浏览器在 Android 上打开 Chrome 应用程序?我能够打开该应用程序,但它不会将用户重定向到正确的页面。这是我尝试过的:
<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.
我看到我可能需要形成一个意图 URL,但我希望有比这更简单的方法。
This is supposed to be from a web page and there is no web view involved.
这应该来自网页,并且不涉及网络视图。
回答by Cruceo
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:
是的,但如果它没有安装在系统上,您将遇到 ActivityNotFoundException。如果它不可用,您应该通过普通浏览器启动:
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);
}
回答by Ilya Gazman
Here is a solution without a try catch,
if chrome is installed, it will be used. Otherwise, it will go to the device default
这是一个没有try catch,
安装 chrome的解决方案,它将被使用。否则,它将转到设备默认值
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);
}
回答by Maen K Househ
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
并基于此使用 BOM API 更新窗口位置
window.location.href = 'googlechrome://navigate?url='+ link;
回答by karan
I opened my Safe Browser App with getting package name from Google play, the same way you can open for chrome by default also:
我打开了我的安全浏览器应用程序,并从 Google Play 获取包名,默认情况下也可以打开 Chrome 浏览器:
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.
你可以com.cloudacl
用这个替换包com.android.chrome
来打开chrome。
回答by Arvind Chourasiya
I am using below code to open Chrome browser
from Android app
我正在使用以下代码Chrome browser
从 Android 应用程序打开
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);
}
回答by Aftab Alam
Android Kotlin
安卓科特林
Try this below code for intent to chrome browser in kotlin.
试试下面的代码,以便在 kotlin 中使用 chrome 浏览器。
val uri = Uri.parse("googlechrome://navigate?url="+"https://stackoverflow.com/")
val i = Intent(Intent.ACTION_VIEW, uri)
if (i.resolveActivity([email protected]) == null) {
i.data = Uri.parse("https://stackoverflow.com/")
}
[email protected](i)
回答by josedlujan
Your xml:
你的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
:
你的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");
}
}
回答by Tim Smith
The Intent solutions above no longer seem to work.
上面的意图解决方案似乎不再有效。
The following works for me ..
以下对我有用..
document.location = 'googlechrome://navigate?url=www.tovia.com/';
document.location = 'googlechrome://navigate?url=www.tovia.com/';