android - How to make a phone call from webview
Asked Answered
H

4

13

In my app, i am opening the url using webview. This url opens the some page that contains some phone numbers.Now i want to make a phone call without open the phone dialer if u click on the phone number. is it possible? please can anybody help me.

thanks

Hayden answered 23/2, 2012 at 6:47 Comment(0)
P
21
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;
    }
Ptolemy answered 23/2, 2012 at 7:12 Comment(1)
where do i put it?Deland
M
11

Thanks JackTurky! Here's slightly more, to show how it fits with 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;
        }           
    });
Mucoviscidosis answered 14/5, 2014 at 14:2 Comment(0)
C
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;

    }
Cremator answered 21/2, 2019 at 12:26 Comment(0)
J
0

shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean is now depricated.
Use shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean instead.

override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
    return if (request?.url?.scheme?.startsWith("tel") == true) {
        val intent: Intent = Intent(Intent.ACTION_DIAL, request.url)
        view?.context?.startActivity(intent)
        true
    } else {
        super.shouldOverrideUrlLoading(view, request)
    }
}
Jennifer answered 5/8 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.