記錄一下webview的使用過程,webview呼叫原生
阿新 • • 發佈:2018-12-21
先記錄一下我用webview時候setting的一些設定,這些設定我都是無腦貼上:
WebSettings settings = webView.getSettings(); // 設定與Js互動的許可權 settings.setJavaScriptEnabled(true); // 設定允許JS彈窗 settings.setJavaScriptCanOpenWindowsAutomatically(true); // 設定縮放級別 settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM); // 支援縮放 settings.setSupportZoom(true); settings.setDomStorageEnabled(true);//開啟DOM storage API功能 settings.setAllowFileAccess(true); // 將網頁內容以單列顯示 settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
然後後來遇到一個問題 就是載入的網頁佈局亂了,經過查資料需要設定這個
settings.setUseWideViewPort(false);//這裡需要設定為true,才能讓Webivew支援<meta>標籤的viewport屬性 接下來記錄一下重寫的Webview中的方法和作用 webView.setWebViewClient(new WebViewClient(){};
以下重寫的是WebViewClient中的方法:
@Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error);//錯誤提示,當網頁斷網載入失敗我們可以做一些相關跳轉,避免出現斷網的不友好圖示: LogUtils.LOG("ceshi","dsfa"+error,"斷網"); Intent intent=new Intent(MainActivity.this,KongActivity.class); startActivity(intent); finish(); }
webView.setWebChromeClient(new WebChromeClient(){}
以下是重寫WebChromeClient中的方法
@Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); if(newProgress==100){ mPrigressBer.setVisibility(View.GONE);//載入完網頁進度條消失 LogUtils.LOG("ceshi",webView.getUrl(),"網址"); uuuu=webView.getUrl();//可以得到載入的網頁,我們可以判斷當載入的網頁是主頁時清除掉歷史,有效防止迴圈後退 if(webView.getUrl().contains("https://www.baidu.com") ){ webView.clearHistory();//清楚webview載入的歷史,點選返回直接退出 } } else{ mPrigressBer.setVisibility(View.VISIBLE);//開始載入網頁時顯示進度條 mPrigressBer.setProgress(newProgress);//設定進度值 } }
我的專案裡還有一個需求上傳圖片,所以我在WebChromeClient中呼叫了這幾個方法
public final static int FILECHOOSER_RESULTCODE = 1; public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2; public ValueCallback<Uri> mUploadMessage; private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } public ValueCallback<Uri[]> mUploadMessageForAndroid5; private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) { mUploadMessageForAndroid5 = filePathCallback; Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5); }
以及重寫了
@Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { LogUtils.LOG("ceshi","圖片選擇","tupian"); onenFileChooseImpleForAndroid(filePathCallback); return true; }
最後重寫了onactivityresult方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != RESULT_OK ? null: data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){ if (null == mUploadMessageForAndroid5) return; Uri result = (data == null || resultCode != RESULT_OK) ? null: data.getData(); if (result != null) { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result}); } else { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{}); } mUploadMessageForAndroid5 = null; } }
完整程式碼:
webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); if(newProgress==100){ mPrigressBer.setVisibility(View.GONE);//載入完網頁進度條消失 LogUtils.LOG("ceshi",webView.getUrl(),"網址"); uuuu=webView.getUrl(); if(webView.getUrl().contains("https://eosmytoken.io/app/index/index.html")|| webView.getUrl().contains("https://eosmytoken.io/app/news/index.html")|| webView.getUrl().contains("https://eosmytoken.io/app/user/index.html") // ||webView.getUrl().contains("http://app.ete-coin.com/app/user/index.html") ){ webView.clearHistory(); } } else{ mPrigressBer.setVisibility(View.VISIBLE);//開始載入網頁時顯示進度條 mPrigressBer.setProgress(newProgress);//設定進度值 } } @Override public void onReceivedTitle(WebView view, String title) { super.onReceivedTitle(view, title); LogUtils.LOG("ceshi",title,"網址網址網址"); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) { if(title.contains("找不到")){ Intent intent=new Intent(MainActivity.this,KongActivity.class); startActivity(intent); finish(); } } } //擴充套件瀏覽器上傳檔案 //3.0++版本 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { openFileChooserImpl(uploadMsg); } //3.0--版本 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooserImpl(uploadMsg); } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooserImpl(uploadMsg); } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { LogUtils.LOG("ceshi","圖片選擇","tupian"); onenFileChooseImpleForAndroid(filePathCallback); return true; } @Override//這裡是js互動。和web協商可以做呼叫原生操作 注意:重寫這個方法後,web的alert不會彈出來 public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Uri uri = Uri.parse(message); if ( uri.getScheme().equals("mytoken")) { // 如果 authority = 預先約定協議裡的 webview,即代表都符合約定的協議 // 所以攔截url,下面JS開始呼叫Android需要的方法 Log.i("ceshi",uri.getAuthority()+".....2"); // webView.stopLoading(); if(uri.getAuthority().equals("scan")){ //做相關操作 }else { //做相關操作 } result.cancel(); return true; } return true; } });
public final static int FILECHOOSER_RESULTCODE = 1; public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2; public ValueCallback<Uri> mUploadMessage; private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } public ValueCallback<Uri[]> mUploadMessageForAndroid5; private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) { mUploadMessageForAndroid5 = filePathCallback; Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != RESULT_OK ? null: data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){ if (null == mUploadMessageForAndroid5) return; Uri result = (data == null || resultCode != RESULT_OK) ? null: data.getData(); if (result != null) { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result}); } else { mUploadMessageForAndroid5.onReceiveValue(new Uri[]{}); } mUploadMessageForAndroid5 = null; }}