When i load url into WebView and try to open links, some of them shows an error page like:
net::ERR_UNKNOWN_URL_SCHEME intent://maps.yandex.ru?utm_med
I found a solution. This question and this documentation helps me to understand situation.
As a result, i've written a link handler which follow this logic:
The code is:
mWebView.setWebViewClient(new CustomWebViewClient());
//...
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
if (url.startsWith("http")) return false;//open web links as usual
//try to find browse activity to handle uri
Uri parsedUri = Uri.parse(url);
PackageManager packageManager = getActivity().getPackageManager();
Intent browseIntent = new Intent(Intent.ACTION_VIEW).setData(parsedUri);
if (browseIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(browseIntent);
return true;
}
//if not activity found, try to parse intent://
if (url.startsWith("intent:")) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
getActivity().startActivity(intent);
return true;
}
//try to find fallback url
String fallbackUrl = intent.getStringExtra("browser_fallback_url");
if (fallbackUrl != null) {
webView.loadUrl(fallbackUrl);
return true;
}
//invite to install
Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(
Uri.parse("market://details?id=" + intent.getPackage()));
if (marketIntent.resolveActivity(packageManager) != null) {
getActivity().startActivity(marketIntent);
return true;
}
} catch (URISyntaxException e) {
//not an intent uri
}
}
return true;//do nothing in other cases
}
}
Maybe it needs some cleanup, but it can be helpful. Please tell me if you know an easier way of doing this, i still looking for the best solution.