package com.oct.octim; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class WebViewActivity extends AppCompatActivity { private WebView webView; private String url = "https://im.opencodetiger.cn/EnterIm/VisitorEnter?ShopUserID=66666666666666666&VisitType=Goods&MobileNum=15675178605&IsShowTitleHeader=false&BuyerUserID=0&BuyerNick=&BuyerHeaderImg=&VisitorMemo=某某商城店铺的访客&TopTitle=测试接入&TopAHref=https://impc.opencodetiger.com&ShowItemHeaderImgUrl=https://im.opencodetiger.cn/Assets/Imgs/Icon/my_shopmsg.png&ShowTitle=某某商城店铺客服&ShowTitleSub=你好,欢迎访问某某店铺,现在可以咨询聊天啦&OsType=Wap&rnd=0.09382889672423"; private String Tag = "OctIM"; private ValueCallback uploadFile; private ValueCallback uploadFiles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_web_view); //初始化WebView initWebView(); } /** * 初始化WebView */ private void initWebView() { webView = (WebView) findViewById(R.id.wv_webview); //支持javascript webView.getSettings().setJavaScriptEnabled(true); // 设置可以支持缩放 webView.getSettings().setSupportZoom(true); // 设置出现缩放工具 webView.getSettings().setBuiltInZoomControls(true); //扩大比例的缩放 webView.getSettings().setUseWideViewPort(true); //自适应屏幕 webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); webView.getSettings().setLoadWithOverviewMode(true); //加载具体网页 webView.loadUrl(url); //如果不设置WebViewClient,请求会跳转系统浏览器 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { //返回false,意味着请求过程里,不管有多少次的跳转请求(即新的请求地址),均交给webView自己处理,这也是此方法的默认处理 //返回true,说明你自己想根据url,做新的跳转,比如在判断url符合条件的情况下,我想让webView加载http://ask.csdn.net/questions/178242 //---------------处理WebView中拨打电话---------------// String _requestURL = request.getUrl().toString(); Log.d("OctIM", "执行啦11URL=" + request.getUrl().toString()); //判断用户单击的是那个超连接 String tag = "tel"; if (_requestURL.contains(tag)) { String mobile = _requestURL.substring(_requestURL.lastIndexOf("/") + 1); Log.e("mobile----------->", mobile); Intent mIntent = new Intent(Intent.ACTION_CALL); Uri data = Uri.parse(mobile); mIntent.setData(data); //Android6.0以后的动态获取打电话权限 if (ActivityCompat.checkSelfPermission(WebViewActivity.this, android.Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) { startActivity(mIntent); //这个超连接,java已经处理了,webview不要处理 return true; } else { //申请权限 ActivityCompat.requestPermissions(WebViewActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1); return true; } } //---------------处理WebView中拨打电话---------------// return false; } }); //-----------------处理WebView中不能上传文件的问题---------------// webView.setWebChromeClient(new WebChromeClient() { // // For Android 3.0+ // public void openFileChooser(ValueCallback uploadMsg, String acceptType) { // Log.i("test", "openFileChooser 1"); // WebViewActivity.this.uploadFile = uploadMsg; // openFileChooseProcess(); // } // // // For Android < 3.0 // public void openFileChooser(ValueCallback uploadMsgs) { // Log.i("test", "openFileChooser 2"); // WebViewActivity.this.uploadFile = uploadMsgs; // openFileChooseProcess(); // } // // // For Android > 4.1.1 // public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) { // Log.i("test", "openFileChooser 3"); // WebViewActivity.this.uploadFile = uploadMsg; // openFileChooseProcess(); // } // For Android >= 5.0 public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { Log.i("test", "openFileChooser 4:" + filePathCallback.toString()); WebViewActivity.this.uploadFiles = filePathCallback; openFileChooseProcess(); return true; } }); } private void openFileChooseProcess() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult(Intent.createChooser(i, "上传文件"), 0); } @Override protected void onResume() { super.onResume(); webView.onResume(); } @Override protected void onPause() { super.onPause(); webView.onPause(); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0) { if (resultCode == RESULT_OK) { Log.d("","执行了RESULT_OK"); if (null != uploadFile) { Uri result = data == null ? null : data.getData(); uploadFile.onReceiveValue(result); uploadFile = null; Log.d("","执行了uploadFile"); } if (null != uploadFiles) { Uri result = data == null ? null : data.getData(); uploadFiles.onReceiveValue(new Uri[]{result}); uploadFiles = null; Log.d("","执行了uploadFiles"); } } else if (resultCode == RESULT_CANCELED) { Log.d("","执行了RESULT_CANCELED"); if (null != uploadFile) { uploadFile.onReceiveValue(null); uploadFile = null; Log.d("","执行了uploadFile"); } if (null != uploadFiles) { uploadFiles.onReceiveValue(null); uploadFiles = null; Log.d("","执行了uploadFile"); } } } } }