简体   繁体   English

如何在 android webview 中打开“电话:111111111”链接的应用程序

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

I have problem to open direct contact link in android webview Error: "我无法打开 android 中的直接联系链接 webview 错误:“

Webpage not available网页无法显示

The webpage at 8806288365 could not be loaded because:无法加载 8806288365 的网页,原因是:

net::ERR_UNKNOWN_URL_SCHEME" Please help.. Thnaks net::ERR_UNKNOWN_URL_SCHEME" 请帮忙.. Thnaks

If any link like, whatsapp: open in android webview, then I want to open dialer and whatsapp like this如果有任何链接,whatsapp:在 android webview 中打开,那么我想像这样打开拨号器和 whatsapp

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.要处理 Android WebView 中的“tel:”链接,您需要覆盖 WebView 的 URL 加载行为并拦截“tel:”URL 方案以打开电话拨号器。

Create a Custom WebViewClient and implement as below-创建一个 Custom WebViewClient 并实现如下 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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