Webview email link (mailto)

前端 未结 3 766
暖寄归人
暖寄归人 2021-01-31 15:39

I have a view and view the site has malito code to send email. When I open the link opens in an error. I want that when I open the link opens Gmail app or another email applic

3条回答
  •  被撕碎了的回忆
    2021-01-31 16:08

    Note : - After Android Nougat 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; 
    } 
    

提交回复
热议问题