簡體   English   中英

Android WebView:電話:地理位置:Mailto:正確處理

[英]Android WebView: Tel: Geo: Mailto: Proper Handling

有人可以幫忙解釋如何使用WebView正確處理“電話:地理位置:”和“郵件收件人:”鏈接。

當前,所有鏈接均導致“無法顯示頁面”錯誤。

以下是我使用的代碼,它與其他建議的解決方案結合在一起:

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.loadUrl("http://www.google.com");
mWebView.setWebViewClient(new HelloWebViewClient()); 

}
private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        if (url.startsWith("tel:")) { 
            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url))); 
            return true; 
        } else if (url.startsWith("mailto:")) { 
            url = url.replaceFirst("mailto:", ""); 
            url = url.trim(); 
            Intent i = new Intent(Intent.ACTION_SEND); 
            i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL, new String[]{url}); 
            startActivity(i); 
            return true; 
        } else if (url.startsWith("geo:")) { 
            Intent searchAddress = new Intent(Intent.ACTION_VIEW, Uri.parse(url));  
            startActivity(searchAddress);        
            return true; 
        } else { 
            view.loadUrl(url); 
            return true; 
        } 
    } 
}

}

這個答案對我Intent.ACTION_VIEW ,並且您可以在每種情況下都使用Intent.ACTION_VIEW ,因為它會強制設備查找可能顯示給用戶的選項。

該代碼對我有用:(如果使用后退按鈕,則上述代碼不正確)

調用自定義Webview:

view.setWebViewClient(new CustomWebViewClient());

現在擴展WebView:

 private class CustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }
            // Otherwise allow the OS to handle it
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );
            return true;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM