android - 如何从webview拨打电话

时间:2012-02-23 06:47:25

标签: android webview

在我的应用中,我使用webview打开网址。此网址会打开包含一些电话号码的某个页面。现在,如果您点击电话号码,我打算拨打电话而不打开电话拨号。可能吗?请任何人帮助我。

感谢

3 个答案:

答案 0 :(得分:16)

public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("tel:")) { 
                Intent intent = new Intent(Intent.ACTION_DIAL,
                        Uri.parse(url)); 
                startActivity(intent); 
        }else if(url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
        }
        return true;
    }

答案 1 :(得分:3)

谢谢JackTurky!这里稍微详细一点,展示它如何适合webView:

    webView.setWebViewClient(new WebViewClient() {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.startsWith("tel:")) { 
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url)); 
                    startActivity(intent); 
                    return true;
            }
            return false;
        }           
    });

答案 2 :(得分:0)

public boolean shouldOverrideUrlLoading(WebView view, String url) {

        Log.i("TEST","URL 4: "+url);

        if (url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
        }else if (url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_DIAL);

            // Send phone number to intent as data
            intent.setData(Uri.parse(url));

            // Start the dialer app activity with number
            startActivity(intent);
        }

            return true;

    }