简体   繁体   中英

How to open app for "tel:111111111" link in android webview

I have problem to open direct contact link in android webview Error: "

Webpage not available

The webpage at 8806288365 could not be loaded because:

net::ERR_UNKNOWN_URL_SCHEME" Please help.. Thnaks

If any link like, whatsapp: open in android webview, then I want to open dialer and whatsapp like this

My Code:

class WebViewClientDemo extends WebViewClient {
    @Override
    //Keep webview in app when clicking links
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
//        view.loadUrl(url);
//        return true;

        if (url.startsWith("tel:")) {
            // Handle telephone URLs
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(url));
            view.getContext().startActivity(intent);
            return true;
        } else if (url.startsWith("mailto:")) {
            // Handle email URLs
            Intent intent = new Intent(Intent.ACTION_SENDTO);
            intent.setData(Uri.parse(url));
            view.getContext().startActivity(intent);
            return true;
        } else {
            // Load all other URLs within the WebView
            view.loadUrl(url);
            return true;
        }
    }


}

To handle the "tel:" link in an Android WebView, you need to override the WebView's URL loading behavior and intercept the "tel:" URL scheme to open the phone dialer.

Create a Custom WebViewClient and implement as below-

val webView = findViewById<WebView>(R.id.webView)
webView.webViewClient = CustomWebViewClient()
val htmlContent = "<html><a href=\"tel:8806288365\" >phone</a></html>"
webView.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null)

class CustomWebViewClient : WebViewClient() {
    override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
//        return super.shouldOverrideUrlLoading(view, request)
        val url = request?.url?.toString()
        if (url?.startsWith("tel:") == true) {
            val intent = Intent(Intent.ACTION_DIAL)
            intent.data = Uri.parse(url)
            view?.context?.startActivity(intent)
            return true
        }
        return false
    }
}

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