Support for other protocols in Android webview
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:
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.
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;
}
}
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:
<a href="javascript:go('market://your.path.to.market.app')">..</a>
Define a javascript function named go():
<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.addJavascriptInterface(new Handler() {
@Override
public void go(String marketUrl) {
//start market intent here
}
}, "handler");
Handler interface can be defined as follows:
public interface Handler{
public void go(String url);
}