支持 Android webview 中的其他协议
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583264/
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
Support for other protocols in Android webview
提问by user319940
I've created a web view app, the page that is displayed features market:// links but upon clicking them I get the 404 screen along with the error that the protocol is not supported. I've tried looking through documentation but was unable to find anything relating to this. Any help is much appreciated.
我创建了一个 Web 视图应用程序,显示的页面具有市场:// 链接,但在单击它们时,我得到 404 屏幕以及不支持该协议的错误。我试过查看文档,但找不到与此相关的任何内容。任何帮助深表感谢。
回答by sven
For me the JavaScript thing wasn't a solution as the HTML is not under my control. So if you need to control this from the application side, then there is a relative simple solution: Derive from WebViewClient
and inject the implementation using WebView.setWebViewClient()
. All you need to override in your WebViewClient
implementation is the shouldOverrideUrlLoading
method as shown here:
对我来说,JavaScript 不是解决方案,因为 HTML 不在我的控制之下。因此,如果您需要从应用程序端对此进行控制,那么有一个相对简单的解决方案:WebViewClient
使用WebView.setWebViewClient()
. 您需要在WebViewClient
实现中覆盖的shouldOverrideUrlLoading
只是如下所示的方法:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("market://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
For me this works fine.
对我来说这很好用。
回答by Konstantin Burov
For the links to work you have to have the market app installed on your device/emulator. Also your app need to request a permission to access network.
要使链接起作用,您必须在您的设备/模拟器上安装市场应用程序。此外,您的应用程序需要请求访问网络的权限。
UPD: as a workaround you can call java code from within the webview, for example if you generate links like this:
UPD:作为一种解决方法,您可以从 webview 中调用 java 代码,例如,如果您生成这样的链接:
<a href="javascript:go('market://your.path.to.market.app')">..</a>
Define a javascript function named go():
定义一个名为 go() 的 JavaScript 函数:
<script type="text/javascript">
function go(link) {
if (handler) {
handler.go(link);
} else {
document.location = link;
}
}
</script>
You then can pass in a handler object into the WebView:
然后,您可以将处理程序对象传入 WebView:
webview.addJavascriptInterface(new Handler() {
@Override
public void go(String marketUrl) {
//start market intent here
}
}, "handler");
Handler interface can be defined as follows:
Handler接口可以定义如下:
public interface Handler{
public void go(String url);
}
回答by Sathish Kumar
HOPE THIS HELPS YOU
希望这对你有帮助
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
else{
view.loadUrl(url);
return true;
}
}
回答by Jonathan Nolasco Barrientos
Work for me:
为我工作:
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://myweb.com");
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
回答by Tarang Zilpe
It is important to understand how the webview and its clients (webviewclient and webchromeclient) works. Please go through the http://therockncoder.blogspot.in/2014/04/understanding-androids-webchromeclient.html
了解 webview 及其客户端(webviewclient 和 webchromeclient)的工作方式很重要。请通过http://therockncoder.blogspot.in/2014/04/understanding-androids-webchromeclient.html
In the webviewclient's shouldOverrideUrlLoading() method, you can decide if you want to open the link in new browser or within the webview. If you don't override this method, it will by default open the link in a new browser outside of the your android application. If you want to open within webview, override the method as below
在 webviewclient 的 shouldOverrideUrlLoading() 方法中,您可以决定是要在新浏览器中还是在 webview 中打开链接。如果您不覆盖此方法,默认情况下它将在您的 android 应用程序之外的新浏览器中打开链接。如果你想在 webview 中打开,覆盖如下方法
public boolean shouldOverrideUrlLoading(WebView view, String url) { <br>
Log.v("activity", "INSIDE WEBVIEW CLIENT ON shouldOverrideUrlLoading");
view.loadUrl(url);
return false; //need to understand return value based on usage
}
Schemes like whatsapp://send?text=Hello%20World!or market://details?id=xx.xx.xxwill open the corresponding apps automatically if they are opened outside the webview and if that app is installed on the handset.
像whatsapp://send?text=Hello%20World! 这样的方案 或者market://details?id=xx.xx.xx如果在 webview 之外打开相应的应用程序并且如果该应用程序安装在手机上,则会自动打开相应的应用程序。
If you want to open certain links within webview and specific schemes outside webview, you need to override WebChromeClients onCreateWindow() method as explained in the link provided above. It should solve the purpose.
如果您想打开 webview 中的某些链接和 webview 外部的特定方案,您需要覆盖 WebChromeClients onCreateWindow() 方法,如上面提供的链接中所述。它应该解决目的。
回答by Sumit
Instead of adding check for particular scheme, Modifying @sven solution, this will work for all schemes
而不是为特定方案添加检查,修改@sven 解决方案,这将适用于所有方案
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host= Uri.parse(url).getHost();
if (host == null) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
view.loadUrl(url);
return false;
}
回答by Ciff
Simplest solution
最简单的解决方案
Intent newApp = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(newApp);