Android WebView問題匯總以及解決方案

來源:互聯網
上載者:User

標籤:

Android WebView常見問題解決方案匯總:

1、Android webview背景設定為透明無效

webview是一個使用方便、功能強大的控制項,但由於webview的背景顏色預設是白色,在一些場合下會顯得很突兀(比如背景是黑色)。
此時就想到了要把webview的背景設定為透明,這樣就可以與其背景融為一體。
在2.X的平台下,一般設定webview背景為透明的方法如下:
wvContent.setBackgroundColor(0);  
但當程式在4.0上使用時,發現居然這種設定方法無法,即使通過上面設定背景為0,照樣顯示出原來預設的白色背景。
通過網上尋找,發現原來是由於硬體加速導致的,此時就想到了使用代碼關閉當前webview的硬體加速,方法如下:
wvContent.setLayerType(View.LAYER_TYPE_SOFTWARE,null);  
這時發現又有新問題,如果要通過該代碼進行設定,SDK最低版本也要11(android 3.0)無法正常編譯
這時需要在布局檔案中設定 android:layerType="software"  
通過測試,在4.0和2.2都能正常運行,webview透明背景設定成功

2、WebView頁面中播放了音頻,退出Activity後音頻仍然在播放

需要在Activity的onDestory()中調用webView.destroy();  
但是直接調用可能會引起如下錯誤:

06-10 15:01:11.402: E/ViewRootImpl(7502): sendUserActionEvent() mView == null  06-10 15:01:26.818: E/webview(7502): java.lang.Throwable: Error: WebView.destroy() called while still attached!  06-10 15:01:26.818: E/webview(7502): at android.webkit.WebViewClassic.destroy(WebViewClassic.java:4142) 06-10 15:01:26.818: E/webview(7502): at android.webkit.WebView.destroy(WebView.java:707)06-10 15:01:26.818: E/webview(7502): at com.didi.taxi.ui.webview.OperatingWebViewActivity.onDestroy(OperatingWebViewActivity.java:236) 06-10 15:01:26.818: E/webview(7502): at android.app.Activity.performDestroy(Activity.java:5543) 06-10 15:01:26.818: E/webview(7502): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1134) 06-10 15:01:26.818: E/webview(7502): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3619) 06-10 15:01:26.818: E/webview(7502): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3654)06-10 15:01:26.818: E/webview(7502): at android.app.ActivityThread.access$1300(ActivityThread.java:159)06-10 15:01:26.818: E/webview(7502): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369) 06-10 15:01:26.818: E/webview(7502): at android.os.Handler.dispatchMessage(Handler.java:99)06-10 15:01:26.818: E/webview(7502): at android.os.Looper.loop(Looper.java:137)06-10 15:01:26.818: E/webview(7502): at android.app.ActivityThread.main(ActivityThread.java:5419)06-10 15:01:26.818: E/webview(7502): at java.lang.reflect.Method.invokeNative(Native Method)06-10 15:01:26.818: E/webview(7502): at java.lang.reflect.Method.invoke(Method.java:525)06-10 15:01:26.818: E/webview(7502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)03-10 15:01:26.818: E/webview(7502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)06-10 15:01:26.818: E/webview(7502): at dalvik.system.NativeStart.main(Native Method)
如上所示,webview調用destory時,webview仍綁定在Activity上.這是由於自訂webview構建時傳入了該Activity的context對象,因此需要先從父容器中移除webview,然後再銷毀webview:rootLayout.removeView(webView);

3、WebView保留縮放功能但隱藏縮放控制項:

mWebView.getSettings().setSupportZoom(true);  mWebView.getSettings().setBuiltInZoomControls(true);  if (DeviceUtils.hasHoneycomb())  mWebView.getSettings().setDisplayZoomControls(false);  
注意:setDisplayZoomControls是在Android 3.0中新增的API.

4、為WebView自訂錯誤顯示介面:

覆寫WebViewClient中的onReceivedError()方法:

/**  * 顯示自訂錯誤提示頁面,用一個View覆蓋在WebView  */  protected void showErrorPage() {      LinearLayout webParentView = (LinearLayout)mWebView.getParent();      initErrorPage();      while (webParentView.getChildCount() > 1) {        webParentView.removeViewAt(0);      }    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);      webParentView.addView(mErrorView, 0, lp);      mIsErrorPage = true;  }  protected void hideErrorPage() {      LinearLayout webParentView = (LinearLayout)mWebView.getParent();            mIsErrorPage = false;      while (webParentView.getChildCount() > 1) {          webParentView.removeViewAt(0);      }  }    protected void initErrorPage() {      if (mErrorView == null) {          mErrorView = View.inflate(this, R.layout.online_error, null);          Button button = (Button)mErrorView.findViewById(R.id.online_error_btn_retry);          button.setOnClickListener(new OnClickListener() {              public void onClick(View v) {                  mWebView.reload();              }          });          mErrorView.setOnClickListener(null);      }  }  @Override  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {  mErrorView.setVisibility(View.VISIBLE);  super.onReceivedError(view, errorCode, description, failingUrl);  } 

5、WebView cookies清理:

CookieSyncManager.createInstance(this);   CookieSyncManager.getInstance().startSync();   CookieManager.getInstance().removeSessionCookie();   
但是CookieManager已經過時,不建議使用。
文檔中提示The WebView now automatically syncs cookies as necessary.

6、清理cache 和記錄:

webView.clearCache(true);   webView.clearHistory();  m_webview.loadDataWithBaseURL(null, result,"text/html", "utf-8",null);

7、判斷WebView是否已經滾動到頁面底端:

getScrollY()方法返回的是當前可見地區的頂端距整個頁面頂端的距離,也就是當前內容滾動的距離.   
getHeight()或者getBottom()方法都返回當前WebView 這個容器的高度   
getContentHeight 返回的是整個html 的高度,但並不等同於當前整個頁面的高度,因為WebView 有縮放功能, 所以當前整個頁面的高度實際上應該是原始html 的高度再乘上縮放比例. 因此,更正後的結果,準確的判斷方法應該是:   
if(WebView.getContentHeight*WebView.getScale() == (webview.getHeight()+WebView.getScrollY()))
{ //已經處於底端 }   

8、URL攔截:

Android WebView是攔截不到頁面內的fragment跳轉的。但是url跳轉的話,又會引起頁面重新整理,H5頁面的體驗又下降了。只能給WebView注入JS方法了。

9、處理WebView中的非超連結請求(如Ajax請求): 

有時候需要加上要求標頭,但是非超連結的請求,沒有辦法再shouldOverrinding中攔截並用webView.loadUrl(String url,HashMap headers)方法添加要求標頭
目前用了一個臨時的辦法解決:
首先需要在url中加特殊標記/協議, 如在onWebViewResource方法中攔截對應的請求,然後將要添加的要求標頭,以get形式拼接到url末尾
在shouldInterceptRequest()方法中,可以攔截到所有的網頁中資源請求,比如載入JS,圖片以及Ajax請求等等

@SuppressLint("NewApi")  @Override  public WebResourceResponse shouldInterceptRequest(WebView view,String url) {      // 非超連結(如Ajax)請求無法直接添加要求標頭,現拼接到url末尾,這裡拼接一個imei作為樣本      String ajaxUrl = url;      // 如標識:req=ajax      if (url.contains("req=ajax")) {         ajaxUrl += "&imei=" + imei;      }      return super.shouldInterceptRequest(view, ajaxUrl);    }  

10、在頁面中先顯示圖片:

@Override  public void onLoadResource(WebView view, String url) {    mEventListener.onWebViewEvent(CustomWebView.this, OnWebViewEventListener.EVENT_ON_LOAD_RESOURCE, url);      if (url.indexOf(".jpg") > 0) {       hideProgress(); //請求圖片時即顯示頁面       mEventListener.onWebViewEvent(CustomWebView.this, OnWebViewEventListener.EVENT_ON_HIDE_PROGRESS, view.getUrl());       }      super.onLoadResource(view, url);  }  

11、屏蔽掉長按事件 因為webview長按時將會調用系統的複製控制項:    

mWebView.setOnLongClickListener(new OnLongClickListener() {            @Override            public boolean onLongClick(View v) {                return true;            }        }); 

12、在WebView加入 flash支援:

String temp = "<html><body bgcolor=\"" + "black"                  + "\"> <br/><embed src=\"" + url + "\" width=\"" + "100%"                  + "\" height=\"" + "90%" + "\" scale=\"" + "noscale"                  + "\" type=\"" + "application/x-shockwave-flash"                  + "\"> </embed></body></html>";  String mimeType = "text/html";  String encoding = "utf-8";  web.loadDataWithBaseURL("null", temp, mimeType, encoding, "");  

13、WebView 在Android4.4的手機上onPageFinished()回調會多調用一次

需要盡量避免在onPageFinished()中做業務操作,否則會導致重複調用,還有可能會引起邏輯上的錯誤.

14、需要通過擷取Web頁中的title用來設定自己介面中的title及相關問題:

需要給WebView設定 WebChromeClient,並在onReceiveTitle()回調中擷取

WebChromeClient webChromeClient = new WebChromeClient() {                @Override                public void onReceivedTitle(WebView view, String title) {                    super.onReceivedTitle(view, title);                             txtTitle.setText(title);                }            };    


但是發現在小米3的手機上,當通過webview.goBack()回退的時候,並沒有觸發onReceiveTitle(),這樣會導致標題仍然是之前子頁面的標題,沒有切換回來.
這裡可以分兩種情況去處理:
(1) 可以確定webview中子頁面只有二級頁面,沒有更深的層次,這裡只需要判斷當前頁面是否為初始的首頁面,可以goBack的話,只要將標題設定回來即可.
(2)webview中可能有多級頁面或者以後可能增加多級頁面,這種情況處理起來要複雜一些:
因為正常順序載入的情況onReceiveTitle是一定會觸發的,所以就需要自己來維護webview  loading的一個url棧及url與title的映射關係那麼就需要一個ArrayList來保持載入過的url,一個HashMap儲存url及對應的title.
正常順序載入時,將url和對應的title儲存起來,webview回退時,移除當前url並取出將要回退到的web 頁的url,找到對應的title進行設定即可.這裡還要說一點,當載入出錯的時候,比如無網路,這時onReceiveTitle中擷取的標題為 找不到該網頁,因此建議當觸發onReceiveError時,不要使用擷取到的title.

15、WebView因addJavaScriptInterface()引起的安全問題.

這個問題主要是因為會有惡意的js代碼注入,尤其是在已經擷取root許可權的手機上,一些惡意程式可能會利用該漏洞安裝或者卸載應用.
關於詳細的情況可以參考: https://github.com/pedant/safe-java-js-webview-bridge, 該項目利用onJsPrompt() 替代了addJavaScriptInterface(),同時增加了非同步回調,
很好地解決了webview  js注入的安全問題.


歡迎掃描二維碼,互相學習




Android WebView問題匯總以及解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.