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

I found a way to make mailto work in android web browser, but the method is deprecated. Can someone give me the full code snippet of the new method. Here is the method I found on this site

Java 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 does not work when I have a target platform like Android 7.1.1.

+4
source share
1 answer

Android N has the signature of this method:

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

The one that is supported by all versions of Android has the signature of this method:

public boolean shouldOverrideUrlLoading(WebView view, String url)

What to do to make it work on all versions?

you need to override both methods

api, Android N, ... .. API 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;
}

@RequiresApi(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;
}
+16

Source: https://habr.com/ru/post/1668482/


All Articles