简体   繁体   中英

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; 
        } 
    } 
}

}

这个答案对我Intent.ACTION_VIEW ,并且您可以在每种情况下都使用Intent.ACTION_VIEW ,因为它会强制设备查找可能显示给用户的选项。

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM