webView.setWebViewClient(new WebViewClient() { @RequiresApi(api = Build.VERSION_CODES.M) @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { String tag; String mobile = url.substring(url.lastIndexOf("/") + 1);//获取网页的电话号码 Uri uri = Uri.parse( mobile); if (url.contains("tel:")) { tag = "tel:"; usePhone(tag,uri); Log.e("===",mobile); }else if (url.contains("sms:")){ tag = "sms:"; usePhone(tag,uri); }else { view.loadUrl(url); } return true; } }); } public void usePhone(String tag , Uri uri){ if(tag.equals("tel:")){ Intent intent = new Intent(Intent.ACTION_CALL, uri);//拨打电话 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {//判断手机系统是不是6.0及以上 if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1); } else { startActivity(intent); } }else { startActivity(intent); } }else if (tag.equals("sms:")){ Intent mIntent = new Intent(Intent.ACTION_SENDTO,uri);//发送短信(号码自动导入) startActivity(mIntent); } }
WebView调用手机拨号和短信功能(非自己写的网页)
最新推荐文章于 2024-05-22 13:53:48 发布