CJB
CJB

Reputation: 21

Android WebView: Tel: Geo: Mailto: Proper Handling

Can someone please help explain how to handle Tel: Geo: and Mailto: links correctly using WebView.

Currently all links result in a "page cannot be displayed" error.

Below is the code I'm using which was put together from other suggested solutions:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient()); 

}
private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        if (url.startsWith("tel:")) { 
            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); 
            return true; 
        } else if (url.startsWith("mailto:")) { 
            url = url.replaceFirst("mailto:", ""); 
            url = url.trim(); 
            Intent i = new Intent(Intent.ACTION_SEND); 
            i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url}); 
            startActivity(i); 
            return true; 
        } else if (url.startsWith("geo:")) { 
            Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
            startActivity(searchAddress);        
            return true; 
        } else { 
            view.loadUrl(url); 
            return true; 
        } 
    } 
}

}

Upvotes: 2

Views: 3114

Answers (2)

Irshad Khan
Irshad Khan

Reputation: 6046

This Code is Working for me : (Above code is not proper if you will use back button)

Call Custom Webview :

view.setWebViewClient(new CustomWebViewClient());

Now Extend WebView :

 private class CustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }
            // Otherwise allow the OS to handle it
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );
            return true;
        }
    }

Upvotes: 0

Delforas
Delforas

Reputation: 81

This answer worked for me and you can use the Intent.ACTION_VIEW for every case because it forces the device to find the possible choices to display to the user.

Upvotes: 1

Related Questions