簡體   English   中英

如何在 android webview 中打開“電話:111111111”鏈接的應用程序

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

我無法打開 android 中的直接聯系鏈接 webview 錯誤:“

網頁無法顯示

無法加載 8806288365 的網頁,原因是:

net::ERR_UNKNOWN_URL_SCHEME" 請幫忙.. Thnaks

如果有任何鏈接,whatsapp:在 android webview 中打開,那么我想像這樣打開撥號器和 whatsapp

我的代碼:

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


}

要處理 Android WebView 中的“tel:”鏈接,您需要覆蓋 WebView 的 URL 加載行為並攔截“tel:”URL 方案以打開電話撥號器。

創建一個 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