8

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

Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53
M Venkat Naidu
  • 85
  • 1
  • 2
  • 8
  • 1
    Did you look at the Android docs? They have a link to the replacement function right there. boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request) – Gabe Sechan Feb 01 '17 at 05:30
  • Yes,Gabe Sechan I visited android docs .but as I am a newbie I didn't the replacement function completely. so I am asking for the code snippet – M Venkat Naidu Feb 01 '17 at 05:38
  • Thank you for your suggestion Gabe .I called my self newbie because I am a mechanical engineering student trying to create a app for my website.There is no one near to me to teach coding.I completely depend on free internet sources to learn. – M Venkat Naidu Feb 01 '17 at 05:52
  • @MVenkatNaidu check my answer.. – Abhishek Singh Feb 01 '17 at 06:13

1 Answers1

28

Android N and above has this method signature:

public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)

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

public boolean shouldOverrideUrlLoading(WebView view, String url)

What should I do to make it work on all versions?

you need to override both the methods

For every api including Android N+ you need to change your code.

Check this below code. It will target both lower API with N and above

@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;
}
Abhishek Singh
  • 9,008
  • 5
  • 28
  • 53