如何在Webview中实现“ tel:” URL

时间:2019-01-24 06:27:40

标签: android android-webview

我正在构建一个显示我的网站的Webview应用。我的网站包含可点击的手机号码,用户单击时需要打开拨号器。

我经历了这个question

由于我是Android开发的新手,所以我不知道该粘贴代码的确切位置。

这是我的 Mainactivity.java 代码

public class MainActivity extends AppCompatActivity {

private WebView webView;
private ProgressBar mProgressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView= findViewById(R.id.web);
    mProgressBar= findViewById(R.id.progressbar);
    mProgressBar.setMax(100);

    webView.loadUrl("https://");
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());

    webView.setWebChromeClient(new WebChromeClient(){

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            mProgressBar.setProgress(newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
        }

        @Override
        public void onReceivedIcon(WebView view, Bitmap icon) {
            super.onReceivedIcon(view, icon);
        }
    });

}
@Override
public void onBackPressed(){
    if (webView.canGoBack()) {
        webView.goBack();
    }else {
        finish();
    }
}}

1 个答案:

答案 0 :(得分:0)

您需要Override中的shouldOverrideUrlLoading() {strong> setWebViewClient() 方法

示例代码

  webView.setWebChromeClient(new WebChromeClient() {
     public void onProgressChanged(WebView view, int progress) {
        progressBar.setProgress(progress);
     }
   });


    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);               

        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.contains("tel:"))
            {
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }else {
                progressBar.setVisibility(view.VISIBLE);
                view.loadUrl(url);
                return true;
            }
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            progressBar.setVisibility(View.GONE);
        }
    });