M Venkat Naidu M Venkat Naidu - 4 years ago 250
Android Question

Android Web-View shouldOverrideUrlLoading() Deprecated.(Alternative)

I have Found a method to make mailto work in android webview but the method is deprecated.Can any one give me full code snippet of the new method.
Here is the method I found on this site

Java code is below:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.startsWith("tel:")) {
initiateCall(url);
return true;
}
if (url.startsWith("mailto:")) {
sendEmail(url.substring(7));
return true;
}
return false;
}


But it's not working when I have target platform as Android 7.1.1

Answer Source

For lower API use above method for N you need to change your code... Check this below code.. it will target both lower API with N

@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("tel:")) {
     initiateCall(url);
     return true;
  }
   if (url.startsWith("mailto:")) {
     sendEmail(url.substring(7));
     return true;
  }
     return false;
}

@TargetApi(Build.VERSION_CODES.N)
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
    String url=request.getUrl().toString();
    if (url.startsWith("tel:")) {
     initiateCall(url);
     return true;
  }
   if (url.startsWith("mailto:")) {
     sendEmail(url.substring(7));
     return true;
  }
     return false;
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download