webview API

java.lang.Object的
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.AbsoluteLayout
         ↳ android.webkit.WebView

類概述


一個顯示網頁的視圖,這個類在您的Activity上可以簡單的顯示一些在線內容,從而推出自己定製的瀏覽器。它使用Webkit渲染引擎來顯示網頁和其他更多功能包括歷史記錄向前導航,向後,放大和縮小,執行文本搜索。

想要啓用內置的縮放,需要設置WebSettings.setBuiltInZoomControls的(boolean)(介紹在API CUPCAKE中)。請注意,爲了一個webview在你的Activity訪問互聯網和加載網頁,你必須添加到你的Android的Manifest文件中的Internet權限:

<span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><uses-permission</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">android:name</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"android.permission.INTERNET"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span>

這必須是一個元素的子元素。請查看教程: Building Web Apps in WebView.。


基本用法

默認情況下,一個網頁不提供任何瀏覽器的插件,不允許javaScript網頁錯誤不可以被忽略。如果你的目標只是顯示一些網頁作爲用戶界面的一部分,
這可能是好的;用戶不需要與網頁交互。如果你真的想要一個完全成熟的網絡瀏覽器,那麼你可能要調用的是瀏覽器應用程序,而不是一個webview顯
示一個URL意圖。例如:

<span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> uri </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Uri</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">parse</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"http://www.example.com"</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Intent</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> intent </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Intent</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Intent</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">ACTION_VIEW</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> uri</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 startActivity</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">intent</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span>

更多信息請閱讀Intent 意圖.。

要提供一個webview在自己的Activity中,包括在你的佈局,WebView的窗口設置在Activity的 onCreate():方法中;

<span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebView</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> webview </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebView</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">this</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 setContentView</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span>

然後加載所需的網頁:

<span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"></span> // 最簡單的用法:注意異常不會被拋出<span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);"></span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
</span> // 如果載入此頁有錯誤(見下文)<span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);"></span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">loadUrl</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"http://slashdot.org/"</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">

</span>//或者,你也可以加載一個HTML字符串<span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);"></span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> summary </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"<html><body>You scored <b>192</b> points.</body></html>"</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">loadData</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">summary</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"text/html"</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">null</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
</span> // ... 但是請注意這對HTML有限制
 // 更多的信息請看loaddata()和loaddatawithbaseurl()的javadocs文檔<span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);"></span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span>

一個webview有幾個自定義點,在這裏你可以添加你自己的行爲。這些是:

  • 創建和設置WebChromeClient的子類,當瀏覽器的UI發生變化時調用,如:進度條更改、JavaScript的窗口。
    (見Debugging Taskshttp://developer.android.com/tools/debugging/index.html)
  • 創建和設置WebViewClient子類,當底層內容變化時調用,如:錯誤或表單提交。你也可以再次攔截URL調用。
    (通過shouldOverrideUrlLoading())
  • 修改WebSettings,例如:讓JavaScript可用--setJavaScriptEnabled().
  • 增加JavaScript-to-Java接口addJavascriptInterface(Object,String)。
    可以將java對象綁定到WebView,這樣對象可以被網頁的JavaScript控制。

下面是一個更復雜的例子,顯示的錯誤處理,設置和進度通知:

<span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// Let's display the progress in the activity title bar, like the</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span><span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// browser app does.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 getWindow</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">().</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">requestFeature</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Window</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">FEATURE_PROGRESS</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">

 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">getSettings</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">().</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setJavaScriptEnabled</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">true</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">

 </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">final</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Activity</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> activity </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">this</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">;</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setWebChromeClient</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebChromeClient</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">()</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
   </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">public</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> onProgressChanged</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebView</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> view</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">int</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> progress</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
     </span><span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// Activities and WebViews measure progress with different scales.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
     </span><span class="com" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0);">// The progress meter will automatically disappear when we reach 100%</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
     activity</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setProgress</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">progress </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">*</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="lit" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102);">1000</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
   </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">});</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">setWebViewClient</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">new</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebViewClient</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">()</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
   </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">public</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">void</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> onReceivedError</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">WebView</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> view</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="kwd" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">int</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> errorCode</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> description</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">String</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> failingUrl</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">)</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">{</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
     </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Toast</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">makeText</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">activity</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"Oh no! "</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">+</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> description</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">,</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="typ" style="margin: 0px; padding: 0px; color: rgb(102, 0, 102);">Toast</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">LENGTH_SHORT</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">).</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">show</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">();</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
   </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">}</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">});</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">

 webview</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">.</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">loadUrl</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">(</span><span class="str" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"http://slashdot.org/"</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">);</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);">
 </span>


Cookie和窗口管理

爲了安全起見,你的應用程序需要使用自己的緩存,它不共享瀏覽器應用程序的數據。默認情況下,通過HTML打開新窗口的請求被忽略。無論是通過JavaScript或鏈路上的目標屬性打開,這是真的。你可以定製你的 WebChromeClient 提供你自己打開多個窗口的行爲,這可以使你隨意控制他們的任何方式。標準的行爲是要銷燬並重新創建一個活動時,更改設備的方向或任何其他配置。這將導致的WebView重新載入當前頁面。如果你不希望出現這種情況,您可以將您的Activity屏幕方向和keyboardHidden的變化,然後單獨退出WebView。它會自動重新適配自己。運行時如何處理配置更改請閱讀更多信息,Handling Runtime Changes 。

製作支持不同的屏幕密度的網頁

一個的移動設備的屏幕密度是根據在屏幕上的分辨率。低密度的屏幕每英寸的可用像素較少,一個高密度屏幕在每英寸像素效果顯著更多 - 。一個屏
幕的密度是很重要的,因爲,其他條件都相同,在UI元素(例如,一個按鈕),其高度和寬度被定義在屏幕像素方面會出現較大的和較小的密度較低的
屏幕上的更高密度的屏幕上。爲簡單起見,Android摺疊所有實際的屏幕密度爲:高,中,低三個廣義密度。

缺省情況下,web視圖縮放一個網頁,以便它被繪製在中等密度的屏幕上的尺寸相匹配的默認外觀。因此,在高密度屏幕採用1.5倍縮放(因爲它的像素較小)和0.75倍縮放一個低密度畫面上(因爲它的像素大)。開始的WebView支持API級ECLAIR,DOM,CSS,和meta標籤的功能,以幫助您作爲一個web開發目標不同的屏幕密度的屏幕。這裏有一個總結的功能,你可以用它來處理不同的屏幕密度:

  •  window.devicePixelRatio  DOM屬性。此屬性的值指定了默認的比例因子,用於當前設備。例如,如果window.devicePixelRatio值是“1.0”,則該設備被認爲一箇中等密度(MDPI)的移動設備和默認縮放係數不施加到在網頁;如果該值是“1.5”,則該設備是認爲是一個高密度的移動設備(HDPI)和頁面內容進行縮放1.5倍,如果該值是“0.75”,則該設備被認爲一個低密度的移動設備(LDPI)和內容縮放0.75倍。但是,如果您指定“目標densitydpi”元屬性(下面討論),那麼你可以停止這個默認的縮放行爲。
  •  -webkit-device-pixel-ratio CSS樣式查詢。使用指定的屏幕密度,該樣式表是分別相應的值必須是“0.75”,“1”,或“1.5”,表示方式是低密度
    ,中密度或高密度的設備,屏幕,分別。比如說:example:
    <span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><link</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">rel</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"stylesheet"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">media</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"screen and (-webkit-device-pixel-ratio:1.5)"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">href</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"hdpi.css"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span>

    樣式表僅用於設備的屏幕像素比爲1.5,這是高密度的像素比例。

  • 對於viewport meta標籤的target-densitydpi 屬性. 你可以指定的Web頁面的目標密度,使用以下值:默認縮放將永遠不會發生:
    • device-dpi -使用設備的原始分辨率爲目標的DPI默認縮放不會發生.
    • high-dpi -使用hdpi、低密度的屏幕縮小適當.
    • medium-dpi - 使用mdpi. 高密度屏幕的擴大和低密度屏幕縮小
    • low-dpi -使用ldpi. 中,高密度屏適當擴大.
    •  -指定要使用的DPI DPI值可接受的值是70-400.

    下面是一個meta標籤指定目標密度的例子

    <span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);"><meta</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">name</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"viewport"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="atn" style="margin: 0px; padding: 0px; color: rgb(136, 34, 136);">content</span><span class="pun" style="margin: 0px; padding: 0px; color: rgb(102, 102, 0);">=</span><span class="atv" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0);">"target-densitydpi=device-dpi"</span><span class="pln" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0);"> </span><span class="tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136);">/></span>

如果你想修改網頁密度, 就使用 -webkit-device-pixel-ratio  CSS3 Media Query   並/或  window.devicePixelRatio DOM 屬性 然後你應該設置 target-densitydpi meta 屬性爲 device-dpi. 這將阻止Android的在你的網頁進行縮放,並允許你爲所有像素密度通過CSS和JavaScript做出必要的調整。

HTML5 視頻 播放支持

爲了支持內聯HTML5視頻在您的應用程序,你需要開啓硬件加速和設置 WebChromeClient,

Summary


Nested Classes
interface WebView.FindListener 監聽查找結果的接口 
class WebView.HitTestResult  
interface WebView.PictureListener 不推薦使用此接口在API級別12。該接口是現在已經過時。  
class WebView.WebViewTransport 返回的WebView跨線程邊界傳輸對象
[Expand]

[Expand]
繼承XML 屬性
From class android.view.ViewGroup
From class android.view.View
常量
String SCHEME_GEO URI scheme for map address.
String SCHEME_MAILTO URI scheme for email address.
String SCHEME_TEL URI scheme for telephone number.
繼承的常量
從階級 android.view.ViewGroup
從類 android.view.View
[展開]
繼承的字段
從類 android.view.View
公共構造函數
的WebView上下文上下文)
一個Context對象構造一個新的WebView。
的WebView上下文背景下的AttributeSet ATTRS)
構造一個新的WebView與佈局參數。
的WebView上下文背景下的AttributeSet ATTRS,INT defStyle)
構造一個新的WebView與佈局參數和默認樣式。
的WebView上下文背景下的AttributeSet ATTRS,INT defStyle,布爾privateBrowsing)
這個構造函數在API等級17是不推薦。私人瀏覽功能不再支持直接通過WebView,將在未來的版本中刪除。不再私自訪問 WebSettings,而改用WebViewDatabaseCookieManager 和WebStorage的進行隱私數據操作和細粒度的控制。

Public Methods
void addJavascriptInterface(Object object, String name)
Java對象提供Web視圖
boolean canGoBack()
獲取WebView一個回溯的歷史事項
boolean canGoBackOrForward(int steps)
頁面是否後退或前進,或給定數目的後退或前進。
boolean canGoForward()
獲取的WebView有一個向前的歷史記錄項。
boolean canZoomIn()
不推薦使用此方法  API level17。這種方法很容易出現不準確的網頁渲染和UI線程之間的競爭條件;建議使用onScaleChanged(WebView, float, float).
boolean canZoomOut()
這種方法被廢棄APIlevel17這種方法很容易由於種族的網頁渲染UI線程之間;建議onScaleChanged(WebView, float, float).
Picture capturePicture()
捕捉當前WebView的圖片
void clearCache(boolean includeDiskFiles)
清除資源緩存.
void clearFormData()
目前的表單域自動刪除彈出如果存在的話
void clearHistory()
告訴WebView清除其內部的向後/向前列表
void clearMatches()
清除由 findAllAsync(String).創建出的顯示強調突出的文本。
void clearSslPreferences()
清除存儲響應與SSL證書錯誤.
void clearView()
清除此Web視圖如果measurespecmeasurespec.exactlyondraw()只有白色的背景onmeasure()將返回0,
void computeScroll()
調用父類請求子類更新其mScrollX 和mScrollY的值.
WebBackForwardList copyBackForwardList()
獲取Web視圖的webbackforwardlist
void destroy()
銷燬WebView的內部狀態.
boolean dispatchKeyEvent(KeyEvent event)
調度下一個視圖的焦點路徑上的關鍵事件..
void documentHasImages(Message response)
查詢文件,看它是否包含任何圖片引用。
abstract void dumpViewHierarchyWithProperties(BufferedWriter out, int level)
Dumps custom children to hierarchy viewer.
static String findAddress(String addr)
獲取一個物理位置的地址的第一個子串
int findAll(String find)
這種方法不推薦 API level 16.   首選findAllAsync(String) 
void findAllAsync(String find)
異步的查找網頁上所有的實例
abstract View findHierarchyView(String className, int hashCode)
Returns a View to enable grabbing screenshots from custom children returned in dumpViewHierarchyWithProperties.
void findNext(boolean forward)
滾動並加亮到下一個匹配的 findAllAsync(String), 直到網頁邊界
void flingScroll(int vx, int vy)
void freeMemory()
通知當前的WebView內存是低的,以便它可以釋放任何可用的內存
SslCertificate getCertificate()
獲取主要頂層頁的SSL證書,或者null,如果沒有證書(該網站是不安全的)。
int getContentHeight()
獲取的HTML內容的高度。
Bitmap getFavicon()
獲取當前頁的圖標
WebView.HitTestResult getHitTestResult()
Gets a HitTestResult based on the current cursor node.
String[] getHttpAuthUsernamePassword(String host, String realm)
對於一個給定的主機和域檢索HTTP身份驗證憑據.
String getOriginalUrl()
獲取當前頁面的原始URL..
int getProgress()
獲取當前頁面的進度.
float getScale()
不推薦使用此方法在API等級17。此方法很容易造成不準確的網頁渲染和UI線程之間的競爭。建議onScaleChanged(WebView, float, float).
WebSettings getSettings()
獲取WebSettings的對象,用於控制設置此WebView.
String getTitle()
獲取當前頁面的標題.
String getUrl()
獲取當前頁面的URL.
void goBack()
返回這個Web視圖的歷史記錄.
void goBackOrForward(int steps)
從當前的步驟數跳轉到歷史記錄的指定步驟數。.
void goForward()
WebView的歷史記錄前進
void invokeZoomPicker()
當前WebView調用圖形變焦選擇器.
boolean isPrivateBrowsingEnabled()
獲取此WebView私人瀏覽是否已啓用.
void loadData(String data, String mimeType, String encoding)
這個WebView加載 由“data” 給定的數據 構建成的URL
void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
Loads the given data into this WebView, using baseUrl as the base URL for the content.
void loadUrl(String url)
加載此URL.
void loadUrl(String url, Map<StringString> additionalHttpHeaders)
加載給定的URL指定額外的HTTP標頭.
void onChildViewAdded(View parent, View child)
這種方法被廢棄在API level8WebView不再需要viewgroup.onhierarchychangelistener這種方法現在不會實現
void onChildViewRemoved(View p, View child)
這種方法被廢棄在API level8WebView不再需要viewgroup.onhierarchychangelistener這種方法現在不會實現
InputConnection onCreateInputConnection(EditorInfo outAttrs)
創建一個新的InputConnection InputMethod與視圖交互
boolean onGenericMotionEvent(MotionEvent event)
實現這個方法來處理通用的手勢事件
void onGlobalFocusChanged(View oldFocus, View newFocus)
不推薦使用此方法  API level3。不應該執行WebView ViewTreeObserver.OnGlobalFocusChangeListener。這種方法現在什麼也不做。
boolean onHoverEvent(MotionEvent event)
實現這個方法來處理懸停事件.
void onInitializeAccessibilityEvent(AccessibilityEvent event)
初始化 AccessibilityEvent 事件源信息.
void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info)
初始化AccessibilityNodeInfo 關於視圖的信息.
boolean onKeyDown(int keyCode, KeyEvent event)
默認實現 KeyEvent.Callback.onKeyDown(): 如果視圖是有效並且可點擊,執行按下視圖直到 KEYCODE_DPAD_CENTER orKEYCODE_ENTER 釋放, 
boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
默認實現 KeyEvent.Callback.onKeyMultiple(): 總是返回false (不處理這個事件).
boolean onKeyUp(int keyCode, KeyEvent event)
默認 KeyEvent.Callback.onKeyUp(): 執行點擊直到 KEYCODE_DPAD_CENTER orKEYCODE_ENTER 釋放.
void onPause()
暫停WebView和其相關聯的DOM,插件,JavaScript 等任何額外的處理
void onResume()
恢復webview在onPause()前後.
boolean onTouchEvent(MotionEvent event)
實現這個方法處理手勢事件。.
boolean onTrackballEvent(MotionEvent event)
實現這個方法處理軌跡球運動事件.
void onWindowFocusChanged(boolean hasWindowFocus)
當窗口被扣調用或者失去焦點.
boolean overlayHorizontalScrollbar()
獲取水平滾動條是否有疊加樣式
boolean overlayVerticalScrollbar()
Gets whether vertical scrollbar has overlay style.
boolean pageDown(boolean bottom)
滾動WebView頁面大小一半內容
boolean pageUp(boolean top)
向上滾動WebView頁面大小一半內容
void pauseTimers()
暫停webview 所有佈局,解析,javaScript times計時器
boolean performAccessibilityAction(int action, Bundle arguments)
在視圖上執行指定的動作
boolean performLongClick()
如果被定義將調用調用view的OnLongClickListener
void postUrl(String url, byte[] postData)
webview使用"Post"方法加載URL ,附帶postData 數據。
void reload()
重新載入當前URL.
void removeJavascriptInterface(String name)
從這個web視圖中移除先前注入的Java對象.
boolean requestChildRectangleOnScreen(View child, Rect rect, boolean immediate)
一組子類需要將一個特殊的矩形定爲到屏幕上.
boolean requestFocus(int direction, Rect previouslyFocusedRect)
調用該函數來嘗試給予指定的視圖或者其子類焦點和方向和特定矩形. 直到符合要求 bygetDescendantFocusability().
void requestFocusNodeHref(Message hrefMsg)
請求最後圖像元素的URL
void requestImageRef(Message msg)
最後一個接觸的用戶請求的URL的圖像
WebBackForwardList restoreState(Bundle inState)
這從給定的Bundle對象恢復WebView的狀態,
void resumeTimers()
恢復WebViews所有的佈局,解析和JavaScript計時器
void savePassword(String host, String username, String password)
設置爲指定的主機的用戶名和密碼.
WebBackForwardList saveState(Bundle outState)
保存此Web視圖使用的狀態 onSaveInstanceState(Bundle).
void saveWebArchive(String filename)
保存當前視圖作爲Web存檔.
void saveWebArchive(String basename, boolean autoname, ValueCallback<String> callback)
保存當前視圖作爲Web存檔
void setBackgroundColor(int color)
設置視圖的背景顏色
void setCertificate(SslCertificate certificate)
這種方法被廢棄APIlevel17調用這個函數沒有有用的效果將在以後的版本中被忽略.
void setDownloadListener(DownloadListener listener)
當內容不能被渲染引擎處理和不應該下載時註冊一個接口來使用
void setFindListener(WebView.FindListener listener)
在網頁上查找操作進展通知註冊監聽器
void setHorizontalScrollbarOverlay(boolean overlay)
指定水平滾動條是否有疊加樣式
void setHttpAuthUsernamePassword(String host, String realm, String username, String password)
爲一個給定的主機和領域存儲HTTP身份驗證憑據
void setInitialScale(int scaleInPercent)
設置WebView初始比例
void setLayerType(int layerType, Paint paint)

爲視圖的層指定類別.

void setLayoutParams(ViewGroup.LayoutParams params)
設置此視圖關聯的佈局參數
void setMapTrackballToArrowKeys(boolean setMap)
默認的情況下不推薦使用此方法    API level17。,實際將在未來的版本中支持.
void setNetworkAvailable(boolean networkUp)
通知WebView的網絡狀態
void setOverScrollMode(int mode)
設置該視圖的滾動模式
void setPictureListener(WebView.PictureListener listener)
不推薦使用此方法 API level12。這個方法現在已經過時。.
void setScrollBarStyle(int style)

指定滾動條的樣式.

void setVerticalScrollbarOverlay(boolean overlay)
指定垂直滾動條是否疊加樣式.
void setWebChromeClient(WebChromeClient client)
設置腳本處理.
void setWebViewClient(WebViewClient client)
設置WebViewClient會接收到各種通知和請求
boolean shouldDelayChildPressedState()
如果是按下的狀態應推遲到子類或其子類下返回true
boolean showFindDialog(String text, boolean showIme)
在這個頁面開始查找文本
void stopLoading()
停止當前的加載.
boolean zoomIn()
執行放大該webView
boolean zoomOut()
執行縮小該webview
Protected Methods
int computeHorizontalScrollOffset()

計算的水平滾動條上的滑塊水平範圍的水平偏移量

int computeHorizontalScrollRange()

計算水平滾動條的水平距離

int computeVerticalScrollExtent()

計算的水平滾動條上的滑塊垂直範圍內的垂直距離

int computeVerticalScrollOffset()

計算的垂直滾動條上的滑塊水平範圍的垂直偏移

int computeVerticalScrollRange()

計算的垂直滾動條垂直距離.

void onAttachedToWindow()
當這個視圖連接一個窗口則調用.
void onConfigurationChanged(Configuration newConfig)
目前正在使用的應用程序的資源配置改變時調用
void onDetachedFromWindow()
當這個視圖脫離一個窗口則調用.
void onDraw(Canvas canvas)
實現繪製
void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
系統的一個視圖的焦點狀態改變調用
void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

測量視圖和它的內容來確定測量寬度和測量高度.

void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY)
調用 overScrollBy(int, int, int, int, int, int, int, int, boolean) 迴應滾動操作的結果
void onScrollChanged(int l, int t, int oldl, int oldt)
當此視圖內部內容滾動時回調此響應 
void onSizeChanged(int w, int h, int ow, int oh)
當視圖的大小改變期間回調
void onVisibilityChanged(View changedView, int visibility)
當視圖可見狀態或視圖的父類可見狀態改變.
void onWindowVisibilityChanged(int visibility)
當窗口所包含的可見狀態改變回調 (between GONEINVISIBLE, and VISIBLE).



WebView的設計中,不是什麼事都要WebView類乾的,有些雜事是分給其他人的,這樣WebView專心幹好自己的解析、渲染工作就行了。WebViewClient就是幫助WebView處理各種通知、請求事件的,WebChromeClient是輔助WebView處理Javascript的對話框,網站圖標,網站title,加載進度等。

使用WebView時需在AndroidManifest.xml添加如下權限,否則會出Web page not available錯誤。:

<span class="sy0" style="color: rgb(102, 204, 102);"><</span>uses<span class="sy0" style="color: rgb(102, 204, 102);">-</span>permission android<span class="sy0" style="color: rgb(102, 204, 102);">:</span>name<span class="sy0" style="color: rgb(102, 204, 102);">=</span><span class="st0" style="color: rgb(255, 0, 0);">"android.permission.INTERNET"</span> <span class="sy0" style="color: rgb(102, 204, 102);">/></span>
 
<span class="sy0" style="color: rgb(102, 204, 102);"><</span>uses<span class="sy0" style="color: rgb(102, 204, 102);">-</span>permission android<span class="sy0" style="color: rgb(102, 204, 102);">:</span>name<span class="sy0" style="color: rgb(102, 204, 102);">=</span><span class="st0" style="color: rgb(255, 0, 0);">"android.permission.ACCESS_NETWORK_STATE"</span> <span class="sy0" style="color: rgb(102, 204, 102);">/></span>(有的甚至要加這個權限)

設置WebView基本信息:

mWebview.<span class="me1" style="color: rgb(0, 102, 0);">getSettings</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span>.<span class="me1" style="color: rgb(0, 102, 0);">setJavaScriptEnabled</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span><span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//設置支持Javascript</span>
requestFocus<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span><span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//如果不設置,則在點擊網頁文本輸入框時,不能彈出軟鍵盤及不響應其他的一些事件。</span>
<span class="kw1" style="color: rgb(177, 177, 0);">this</span>.<span class="me1" style="color: rgb(0, 102, 0);">setScrollBarStyle</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>SCROLLBARS_OUTSIDE_OVERLAY<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
mWebView.<span class="me1" style="color: rgb(0, 102, 0);">loadUrl</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>“http<span class="sy0" style="color: rgb(102, 204, 102);">:</span><span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//www.google.com“); //互聯網用</span>
mWebView.<span class="me1" style="color: rgb(0, 102, 0);">loadUrl</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>“file<span class="sy0" style="color: rgb(102, 204, 102);">:</span><span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">///android_asset/XX.html“);//本地文件用  本地文件存放在:assets文件中</span>

一、基本用法

1、設置縮放頁面自適應

ws.<span class="me1" style="color: rgb(0, 102, 0);">setSupportZoom</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// support zoom</span>
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//ws.setDefaultZoom(WebSettings.ZoomDensity.FAR) ;</span>
ws.<span class="me1" style="color: rgb(0, 102, 0);">setBuiltInZoomControls</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//page auto adapter</span>
ws.<span class="me1" style="color: rgb(0, 102, 0);">setUseWideViewPort</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
ws.<span class="me1" style="color: rgb(0, 102, 0);">setLoadWithOverviewMode</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>

2、頁內跳轉

WebView添加一個事件監聽對象(WebViewClient)並重寫其中的一些方法shouldOverrideUrlLoading:對網頁中超鏈接按鈕的響應。當按下某個連接時WebViewClient會調用這個方法,並傳遞參數:按下的url。

mWebView.<span class="me1" style="color: rgb(0, 102, 0);">setWebViewClient</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw1" style="color: rgb(177, 177, 0);">new</span> WebViewClient()<span class="br0" style="color: rgb(102, 204, 102);">{</span>
@Override
<span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">boolean</span> shouldOverrideUrlLoading<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span> view, <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> url<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span> 
 <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//重寫此方法表明點擊網頁裏面的鏈接還是在當前的<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">webview</span>裏跳轉,不跳到瀏覽器那邊</span>
  view.<span class="me1" style="color: rgb(0, 102, 0);">loadUrl</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>url<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
  <span class="kw1" style="color: rgb(177, 177, 0);">return</span> <span class="kw2" style="font-weight: bold;">true</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
<span class="br0" style="color: rgb(102, 204, 102);">}</span>

3、加載進度

<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//此方法可以處理<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">webview</span> 在加載時和加載完成時一些操作</span>
mWebView.<span class="me1" style="color: rgb(0, 102, 0);">setWebChromeClient</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw1" style="color: rgb(177, 177, 0);">new</span> WebChromeClient()<span class="br0" style="color: rgb(102, 204, 102);">{</span>
   @Override
   <span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">void</span> onProgressChanged<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span> view, <span class="kw4" style="color: rgb(153, 51, 51);">int</span> newProgress<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
      <span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>newProgress<span class="sy0" style="color: rgb(102, 204, 102);">==</span><span class="nu0" style="color: rgb(204, 102, 204);">100</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">{</span> 
      <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// 這裏是設置activity的標題, 也可以根據自己的需求做一些其他的操作</span>
        title.<span class="me1" style="color: rgb(0, 102, 0);">setText</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>“加載完成”<span class="br0" style="color: rgb(102, 204, 102);">)</span>;
      <span class="br0" style="color: rgb(102, 204, 102);">}</span><span class="kw1" style="color: rgb(177, 177, 0);">else</span><span class="br0" style="color: rgb(102, 204, 102);">{</span>
        title.<span class="me1" style="color: rgb(0, 102, 0);">setText</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>“加載中…….”<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
      <span class="br0" style="color: rgb(102, 204, 102);">}</span>
  <span class="br0" style="color: rgb(102, 204, 102);">}</span>
<span class="br0" style="color: rgb(102, 204, 102);">}</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>

4、錯誤處理

5、獲取網站圖標

參考stackoverflow.

<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//先在onCreate()方法裏設置icon存儲的路徑</span>
WebIconDatabase.<span class="me1" style="color: rgb(0, 102, 0);">getInstance</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span>.<span class="me1" style="color: rgb(0, 102, 0);">open</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>getDir<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="st0" style="color: rgb(255, 0, 0);">"icons"</span>, MODE_PRIVATE<span class="br0" style="color: rgb(102, 204, 102);">)</span>.<span class="me1" style="color: rgb(0, 102, 0);">getPath</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//再設置<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">webview</span>的WebChromeClient</span>
mWebView.<span class="me1" style="color: rgb(0, 102, 0);">setWebChromeClient</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw1" style="color: rgb(177, 177, 0);">new</span> MyWebChromeClient<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//最後MyWebChromeClient中重寫以下方法</span>
@Override
<span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">void</span> onReceivedIcon<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span> view, Bitmap icon<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
        <span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>icon <span class="sy0" style="color: rgb(102, 204, 102);">!=</span> <span class="kw2" style="font-weight: bold;">null</span> <span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">{</span>
		mIconImgeView.<span class="me1" style="color: rgb(0, 102, 0);">setImageBitmap</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>icon<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	<span class="br0" style="color: rgb(102, 204, 102);">}</span>
<span class="br0" style="color: rgb(102, 204, 102);">}</span>

6、處理back鍵

如果用webview點鏈接看了很多頁以後,如果不做任何處理,點擊系統“Back”鍵,整個瀏覽器會調用finish()而結束自身,如果希望瀏覽的網頁回退而不是退出瀏覽器,需要在當前Activity中處理並消費掉該Back事件。覆蓋Activity類的onKeyDown(int keyCoder,KeyEvent event)方法。

<span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">boolean</span> onKeyDown<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw4" style="color: rgb(153, 51, 51);">int</span> keyCoder,<a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Akeyevent+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">KeyEvent</span></a> event<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">{</span>
    <span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="search_hit" style="background-color: rgb(255, 255, 153);">webView</span>.<span class="me1" style="color: rgb(0, 102, 0);">canGoBack</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="sy0" style="color: rgb(102, 204, 102);">&&</span> keyCoder <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Akeyevent+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">KeyEvent</span></a>.<span class="me1" style="color: rgb(0, 102, 0);">KEYCODE_BACK</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">{</span>
          <span class="search_hit" style="background-color: rgb(255, 255, 153);">webview</span>.<span class="me1" style="color: rgb(0, 102, 0);">goBack</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span> <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//goBack()表示返回<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">webView</span>的上一頁面</span>
          <span class="kw1" style="color: rgb(177, 177, 0);">return</span> <span class="kw2" style="font-weight: bold;">true</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
     <span class="br0" style="color: rgb(102, 204, 102);">}</span>
     <span class="kw1" style="color: rgb(177, 177, 0);">return</span> <span class="kw2" style="font-weight: bold;">false</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
<span class="br0" style="color: rgb(102, 204, 102);">}</span>

7、使用緩存

<span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);">getSettings</span>().<span class="me1" style="color: rgb(0, 102, 0);">setCacheMode</span>(WebSettings.<span class="me1" style="color: rgb(0, 102, 0);">LOAD_CACHE_ELSE_NETWORK</span>);<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// 優先使用緩存</span>
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">WebView</span>.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用緩存</span>
 
 
<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//退出時刪除緩存,可放到 OnDestory()方法裏</span>
 
<a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Afile+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">File</span></a> file <span class="sy0" style="color: rgb(102, 204, 102);">=</span> CacheManager.<span class="me1" style="color: rgb(0, 102, 0);">getCacheFileBaseDir</span>();
<span class="kw1" style="color: rgb(177, 177, 0);">if</span> (file !<span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="kw2" style="font-weight: bold;">null</span> <span class="sy0" style="color: rgb(102, 204, 102);">&&</span> file.<span class="me1" style="color: rgb(0, 102, 0);">exists</span>() <span class="sy0" style="color: rgb(102, 204, 102);">&&</span> file.<span class="me1" style="color: rgb(0, 102, 0);">isDirectory</span>()) <span class="br0" style="color: rgb(102, 204, 102);">{</span> 
    <span class="kw1" style="color: rgb(177, 177, 0);">for</span> (<a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Afile+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">File</span></a> item : file.<span class="me1" style="color: rgb(0, 102, 0);">listFiles</span>()) <span class="br0" style="color: rgb(102, 204, 102);">{</span>
        item.<span class="me1" style="color: rgb(0, 102, 0);">delete</span>();
    <span class="br0" style="color: rgb(102, 204, 102);">}</span>
   file.<span class="me1" style="color: rgb(0, 102, 0);">delete</span>();
<span class="br0" style="color: rgb(102, 204, 102);">}</span>
 
mWebView.<span class="me1" style="color: rgb(0, 102, 0);">clearCache</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
context.<span class="me1" style="color: rgb(0, 102, 0);">deleteDatabase</span>(<span class="st0" style="color: rgb(255, 0, 0);">"<span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">webview</span>.db"</span>);
context.<span class="me1" style="color: rgb(0, 102, 0);">deleteDatabase</span>(<span class="st0" style="color: rgb(255, 0, 0);">"webviewCache.db"</span>);

二、難點重點

1、長按處理

<span class="co3" style="color: rgb(128, 128, 128);">/**
 *  處理長按彈出的上下文菜單事件,包括網頁鏈接,圖片鏈接等...
 */</span>
<span class="kw1" style="color: rgb(177, 177, 0);">class</span> Hao123ContextMenuListener <span class="kw1" style="color: rgb(177, 177, 0);">implements</span> OnCreateContextMenuListener<span class="br0" style="color: rgb(102, 204, 102);">{</span>
	@Override
	<span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">void</span> onCreateContextMenu<span class="br0" style="color: rgb(102, 204, 102);">(</span>ContextMenu menu, <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Aview+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">View</span></a> v,
		ContextMenuInfo menuInfo<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
		<span class="search_hit" style="background-color: rgb(255, 255, 153);">HitTestResult</span> result <span class="sy0" style="color: rgb(102, 204, 102);">=</span>  <span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span><span class="br0" style="color: rgb(102, 204, 102);">)</span>v<span class="br0" style="color: rgb(102, 204, 102);">)</span>.<span class="me1" style="color: rgb(0, 102, 0);">getHitTestResult</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="sy0" style="color: rgb(102, 204, 102);">;</span>
		<span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">null</span> <span class="sy0" style="color: rgb(102, 204, 102);">==</span> result<span class="br0" style="color: rgb(102, 204, 102);">)</span>
			<span class="kw1" style="color: rgb(177, 177, 0);">return</span> <span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
		<span class="kw4" style="color: rgb(153, 51, 51);">int</span> type <span class="sy0" style="color: rgb(102, 204, 102);">=</span> result.<span class="me1" style="color: rgb(0, 102, 0);">getType</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        <span class="kw1" style="color: rgb(177, 177, 0);">if</span> <span class="br0" style="color: rgb(102, 204, 102);">(</span>type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> WebView.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">UNKNOWN_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span>
	             <span class="kw1" style="color: rgb(177, 177, 0);">return</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
	        <span class="kw1" style="color: rgb(177, 177, 0);">if</span> <span class="br0" style="color: rgb(102, 204, 102);">(</span>type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">EDIT_TEXT_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
	             <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// let TextView handles context menu</span>
	             <span class="kw1" style="color: rgb(177, 177, 0);">return</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        <span class="br0" style="color: rgb(102, 204, 102);">}</span>
 
	        MenuInflater inflater <span class="sy0" style="color: rgb(102, 204, 102);">=</span> getActivity<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span>.<span class="me1" style="color: rgb(0, 102, 0);">getMenuInflater</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        inflater.<span class="me1" style="color: rgb(0, 102, 0);">inflate</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">menu</span>.<span class="me1" style="color: rgb(0, 102, 0);">browser_menu</span>, menu<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
	         <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// Show the correct menu group</span>
	        <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> extra <span class="sy0" style="color: rgb(102, 204, 102);">=</span> result.<span class="me1" style="color: rgb(0, 102, 0);">getExtra</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        menu.<span class="me1" style="color: rgb(0, 102, 0);">setGroupVisible</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">id</span>.<span class="me1" style="color: rgb(0, 102, 0);">PHONE_MENU</span>,
	                 type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">PHONE_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        menu.<span class="me1" style="color: rgb(0, 102, 0);">setGroupVisible</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">id</span>.<span class="me1" style="color: rgb(0, 102, 0);">EMAIL_MENU</span>,
	                 type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">EMAIL_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        menu.<span class="me1" style="color: rgb(0, 102, 0);">setGroupVisible</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">id</span>.<span class="me1" style="color: rgb(0, 102, 0);">GEO_MENU</span>,
	                 type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">GEO_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        menu.<span class="me1" style="color: rgb(0, 102, 0);">setGroupVisible</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">id</span>.<span class="me1" style="color: rgb(0, 102, 0);">IMAGE_MENU</span>,
	                 type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">IMAGE_TYPE</span>
	                 <span class="sy0" style="color: rgb(102, 204, 102);">||</span> type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">SRC_IMAGE_ANCHOR_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        menu.<span class="me1" style="color: rgb(0, 102, 0);">setGroupVisible</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>R.<span class="me1" style="color: rgb(0, 102, 0);">id</span>.<span class="me1" style="color: rgb(0, 102, 0);">ANCHOR_MENU</span>,
	                 type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">SRC_ANCHOR_TYPE</span>
	                 <span class="sy0" style="color: rgb(102, 204, 102);">||</span> type <span class="sy0" style="color: rgb(102, 204, 102);">==</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">SRC_IMAGE_ANCHOR_TYPE</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
	        <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// Setup custom handling depending on the type</span>
	        Intent intent<span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	        <span class="kw1" style="color: rgb(177, 177, 0);">switch</span> <span class="br0" style="color: rgb(102, 204, 102);">(</span>type<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
	            <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">PHONE_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
	                 <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//處理撥號</span>
	                 <span class="kw1" style="color: rgb(177, 177, 0);">break</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	
	             <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">EMAIL_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
                         <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//處理Email</span>
	                 <span class="kw1" style="color: rgb(177, 177, 0);">break</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	
	             <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">GEO_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
                         <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//TODO</span>
	                 <span class="kw1" style="color: rgb(177, 177, 0);">break</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	
	             <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">SRC_ANCHOR_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
	             <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">SRC_IMAGE_ANCHOR_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
	             <span class="kw1" style="color: rgb(177, 177, 0);">case</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">WebView</span>.<span class="me1" style="color: rgb(0, 102, 0);"><span class="search_hit" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 153);">HitTestResult</span></span>.<span class="me1" style="color: rgb(0, 102, 0);">IMAGE_TYPE</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
                         <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// 處理長按圖片的菜單項 </span>
	                 <span class="kw1" style="color: rgb(177, 177, 0);">break</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	
	             <span class="kw1" style="color: rgb(177, 177, 0);">default</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span>
	                 <span class="kw1" style="color: rgb(177, 177, 0);">break</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	         <span class="br0" style="color: rgb(102, 204, 102);">}</span>
	<span class="br0" style="color: rgb(102, 204, 102);">}</span>
 
	<span class="kw1" style="color: rgb(177, 177, 0);">private</span> OnMenuItemClickListener menuItemListener <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="kw1" style="color: rgb(177, 177, 0);">new</span> OnMenuItemClickListener<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
		@Override
		<span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">boolean</span> onMenuItemClick<span class="br0" style="color: rgb(102, 204, 102);">(</span><a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Amenuitem+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">MenuItem</span></a> item<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
		 <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// TODO 各菜單項事件處理 		 </span>
		<span class="br0" style="color: rgb(102, 204, 102);">}</span>
	<span class="br0" style="color: rgb(102, 204, 102);">}</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
 
<span class="br0" style="color: rgb(102, 204, 102);">}</span>

2、播放flash

1、判斷是否安裝了flash,若沒有跳轉到市場安裝 http://blog.csdn.net/zircon_1973/article/details/8067041

2、代碼裏設置支持flash播放

WebSettings ws <span class="sy0" style="color: rgb(102, 204, 102);">=</span> mWebView.<span class="me1" style="color: rgb(0, 102, 0);">getSettings</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
ws.<span class="me1" style="color: rgb(0, 102, 0);">setJavaScriptEnabled</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="sy0" style="color: rgb(102, 204, 102);">;</span>
ws.<span class="me1" style="color: rgb(0, 102, 0);">setPluginsEnabled</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>  <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//Flash support</span>
<span class="kw1" style="color: rgb(177, 177, 0);">if</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>DeviceInfo.<span class="me1" style="color: rgb(0, 102, 0);">getSDKVersionNumber</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">></span><span class="nu0" style="color: rgb(204, 102, 204);">7</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="br0" style="color: rgb(102, 204, 102);">{</span>
	<span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//flash support since android 2.2 </span>
	ws.<span class="me1" style="color: rgb(0, 102, 0);">setPluginState</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>PluginState.<span class="me1" style="color: rgb(0, 102, 0);">ON</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
	ws.<span class="me1" style="color: rgb(0, 102, 0);">setAllowFileAccess</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>	
<span class="br0" style="color: rgb(102, 204, 102);">}</span>

AndroidManifest裏相應的Activity加上如下代碼:

 <span class="sc3"><span class="re1" style="color: rgb(0, 0, 255);"><activity</span></span>
<span class="sc3">         <span class="re0" style="color: rgb(0, 0, 255);">android:name</span>=<span class="st0" style="color: rgb(255, 0, 0);">".BrowserActivity"</span></span>
<span class="sc3">         ......</span>
<span class="sc3">         <span class="re0" style="color: rgb(0, 0, 255);">android:hardwareAccelerated</span>=<span class="st0" style="color: rgb(255, 0, 0);">"true"</span></span>
<span class="sc3">         .......</span>
<span class="sc3">  <span class="re2" style="color: rgb(0, 0, 255);">></span></span>

3、實現點擊下載

mWebView.<span class="me1" style="color: rgb(0, 102, 0);">setDownloadListener</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw1" style="color: rgb(177, 177, 0);">new</span> DownloadListener<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
    <span class="kw1" style="color: rgb(177, 177, 0);">public</span> <span class="kw4" style="color: rgb(153, 51, 51);">void</span> onDownloadStart<span class="br0" style="color: rgb(102, 204, 102);">(</span><a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> url, <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> userAgent,<a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> contentDisposition, <a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Astring+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">String</span></a> mimetype,<span class="kw4" style="color: rgb(153, 51, 51);">long</span> contentLength<span class="br0" style="color: rgb(102, 204, 102);">)</span> <span class="br0" style="color: rgb(102, 204, 102);">{</span>
    <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//實現下載的代碼,這裏跳轉到其他瀏覽器下載</span>
    Uri uri <span class="sy0" style="color: rgb(102, 204, 102);">=</span> Uri.<span class="me1" style="color: rgb(0, 102, 0);">parse</span><span class="br0" style="color: rgb(102, 204, 102);">(</span>url<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
    <span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">// Uri uri = Uri.parse("http://www.abc.com/a.apk");如果只下載單個文件</span>
    Intent intent <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="kw1" style="color: rgb(177, 177, 0);">new</span> Intent<span class="br0" style="color: rgb(102, 204, 102);">(</span>Intent.<span class="me1" style="color: rgb(0, 102, 0);">ACTION_VIEW</span>,uri<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
    startActivity<span class="br0" style="color: rgb(102, 204, 102);">(</span>intent<span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
  <span class="br0" style="color: rgb(102, 204, 102);">}</span>
<span class="br0" style="color: rgb(102, 204, 102);">}</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>

4、cookie保存

5、支持視頻播放

可以參考這個項目

6、處理JS事件

三、疑難雜症

1、空白底部

垂直滾動條總是顯示白色軌跡底圖(無法消掉) 在xml中給WebView設置一下屬性發覺不起一點作用:

android<span class="sy0" style="color: rgb(102, 204, 102);">:</span>fadeScrollbars <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="st0" style="color: rgb(255, 0, 0);">"true"</span>
android<span class="sy0" style="color: rgb(102, 204, 102);">:</span>scrollbarStyle <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="st0" style="color: rgb(255, 0, 0);">"outsideOverlay"</span>
android<span class="sy0" style="color: rgb(102, 204, 102);">:</span>scrollbarAlwaysDrawVerticalTrack <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="st0" style="color: rgb(255, 0, 0);">"false"</span>

必須在代碼中對WebView進行設置才能奏效:

<span class="search_hit" style="background-color: rgb(255, 255, 153);">webview</span> .<span class="me1" style="color: rgb(0, 102, 0);">setScrollbarFadingEnabled</span><span class="br0" style="color: rgb(102, 204, 102);">(</span> <span class="kw2" style="font-weight: bold;">true</span> <span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
<span class="search_hit" style="background-color: rgb(255, 255, 153);">webview</span> .<span class="me1" style="color: rgb(0, 102, 0);">setScrollBarStyle</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><a target=_blank href="http://www.google.com/search?hl=en&q=allinurl%3Aview+java.sun.com&btnI=I%27m%20Feeling%20Lucky" style="color: rgb(67, 105, 118); text-decoration: none;"><span class="kw3" style="color: rgb(0, 0, 102);">View</span></a>. <span class="me1" style="color: rgb(0, 102, 0);">SCROLLBARS_INSIDE_OVERLAY</span> <span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>

2、Uncaught TypeError

E<span class="sy0" style="color: rgb(102, 204, 102);">/</span>Web Console<span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="nu0" style="color: rgb(204, 102, 204);">804</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">:</span> Uncaught TypeError<span class="sy0" style="color: rgb(102, 204, 102);">:</span> Cannot call method <span class="st0" style="color: rgb(255, 0, 0);">'getItem'</span> of <span class="kw2" style="font-weight: bold;">null</span> at http<span class="sy0" style="color: rgb(102, 204, 102);">:</span><span class="co1" style="color: rgb(128, 128, 128); font-style: italic;">//z.cdn.turner.com/cnn/tmpl_asset/static/mobile_phone/2273/js/global-min.js:3</span>

加上以下這句即可:

WebSettings settings <span class="sy0" style="color: rgb(102, 204, 102);">=</span> <span class="search_hit" style="background-color: rgb(255, 255, 153);">webView</span>.<span class="me1" style="color: rgb(0, 102, 0);">getSettings</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>
settings.<span class="me1" style="color: rgb(0, 102, 0);">setDomStorageEnabled</span><span class="br0" style="color: rgb(102, 204, 102);">(</span><span class="kw2" style="font-weight: bold;">true</span><span class="br0" style="color: rgb(102, 204, 102);">)</span><span class="sy0" style="color: rgb(102, 204, 102);">;</span>

四、webkit剖析

1、http://mogoweb.net/categories/webkit-research


轉自http://blog.csdn.net/qeqeqe236/article/details/7992043

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章