Android WebView "tel:" links show web page not found
I am trying to get my android webview app to open tel: links to the phone. Every time I open up a telephone link it works great and opens up the phone. However once I am done with my call and go back to the app it is at a page that says "Web Page Not Found tel:0000000000". Then I have to hit the back button once more to get to the page that I clicked the telephone number on.
Is there a way to get it to open the TEL link without trying to find the page in webview as well as opening it up on the phone?
This is code I am using in WebView to override its handling of the TEL and Mailto links:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mailto:") || url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(url));
startActivity(intent);
}
view.loadUrl(url);
return true;
}
Any help would be appreciated. I have spent the last 2 hours scouring goodle and have failed to produce any answers.
OK so I solved the issue I think. I just needed to separate the URL overrides as follows:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
view.reload();
return true;
}
view.loadUrl(url);
return true;
}
Now my regular links work as well as the tel links. I can also add in there for geo: links if I need to and it will not give me the issue that I was having before to open up maps on the phone.
Rather than call loadUrl(url)
, just return false for the URLs that should not be overridden:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( URLUtil.isNetworkUrl(url) ) {
return false;
}
// Otherwise allow the OS to handle it
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
I've found VIEWing tel: works as expected on all phones we've tried it with. No need to special-case it because of the DIAL action.
I've noticed YouTube videos and the like don't work in WebViews, so you may want to detect those as well.
The whole process could probably be generalized for all sorts of URIs by querying the PackageManager for Activities that handle your URI that are also not the embedded browser. That might be overkill and get confused by other installed browsers.
According to the documentation and based on my experience, Intent.ACTION_VIEW
is perfectly fine to parse tel:
, sms:
, smsto:
, mms:
and mmsto:
links.
Here's a 5 in 1:
@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
{
if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mms:") || url.startsWith("mmsto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
Note :- After Android Nouget shouldOverrideUrlLoading
is Deprecated
You need to use shouldOverrideUrlLoading
along with shouldOverrideUrlLoading
for better support. Also you might want to check if url have mailto:
or tel
:, which are used in HTML5 to trigger mail client and phone dial respectively.
A complete solution will look like this now
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("mailto:")) {
//Handle mail Urls
startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));
} else if (url.startsWith("tel:")) {
//Handle telephony Urls
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
} else {
view.loadUrl(url);
}
return true;
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
final Uri uri = request.getUrl();
if (uri.toString().startsWith("mailto:")) {
//Handle mail Urls
startActivity(new Intent(Intent.ACTION_SENDTO, uri));
} else if (uri.toString().startsWith("tel:")) {
//Handle telephony Urls
startActivity(new Intent(Intent.ACTION_DIAL, uri));
} else {
//Handle Web Urls
view.loadUrl(uri.toString());
}
return true;
}
public class MainActivity extends Activity {
private static final String HTML ="<!DOCTYPE html><html><body><a
href='tel:867-5309'>Click here to call!</a></body></html>";
private static final String TEL_PREFIX = "tel:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.webview);
wv.setWebViewClient(new CustomWebViewClient());
wv.loadData(HTML, "text/html", "utf-8");
}
private class CustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView wv, String url) {
if(url.startsWith(TEL_PREFIX)) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
return false;
}
}
}
This was the fix i found. You have to use this method.
wv.setWebViewClient(new CustomWebViewClient());