WebView
extends AbsoluteLayoutimplements ViewGroup.OnHierarchyChangeListener ViewTreeObserver.OnGlobalFocusChangeListener
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 視頻 播放支持
WebChromeClient,
Summary
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
WebView.FindListener | 監聽查找結果的接口 | |||||||||
|
WebView.HitTestResult | ||||||||||
|
WebView.PictureListener | 不推薦使用此接口在API級別12。該接口是現在已經過時。 | |||||||||
|
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
|
公共構造函數 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
一個Context對象構造一個新的WebView。
|
||||||||||
|
構造一個新的WebView與佈局參數。
|
||||||||||
|
構造一個新的WebView與佈局參數和默認樣式。
|
||||||||||
|
這個構造函數在API等級17是不推薦。私人瀏覽功能不再支持直接通過WebView,將在未來的版本中刪除。不再私自訪問
WebSettings ,而改用WebViewDatabase ,CookieManager 和WebStorage 的進行隱私數據操作和細粒度的控制。 |
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
將Java對象提供到Web視圖
|
||||||||||
|
獲取WebView一個回溯的歷史事項
|
||||||||||
|
頁面是否後退或前進,或給定數目的後退或前進。
|
||||||||||
|
獲取的WebView有一個向前的歷史記錄項。
|
||||||||||
|
不推薦使用此方法 API level17。這種方法很容易出現不準確的網頁渲染和UI線程之間的競爭條件;建議使用
onScaleChanged(WebView,
float, float) . |
||||||||||
|
這種方法被廢棄APIlevel17。這種方法很容易由於種族的網頁渲染UI線程之間;建議
onScaleChanged(WebView,
float, float) . |
||||||||||
|
捕捉當前WebView的圖片
|
||||||||||
|
清除資源緩存.
|
||||||||||
|
從目前的表單域自動刪除彈出,如果存在的話
|
||||||||||
|
告訴WebView清除其內部的向後/向前列表。
|
||||||||||
|
|
||||||||||
|
清除存儲響應與SSL證書錯誤.
|
||||||||||
|
清除此Web視圖,如果measurespec不measurespec.exactly。ondraw()將只有白色的背景,並onmeasure()將返回0,
|
||||||||||
|
調用父類請求子類更新其mScrollX 和mScrollY的值.
|
||||||||||
|
獲取此Web視圖的webbackforwardlist。
|
||||||||||
|
銷燬WebView的內部狀態.
|
||||||||||
|
調度下一個視圖的焦點路徑上的關鍵事件..
|
||||||||||
|
查詢文件,看它是否包含任何圖片引用。
|
||||||||||
|
Dumps custom children to hierarchy viewer.
|
||||||||||
|
獲取一個物理位置的地址的第一個子串
|
||||||||||
|
這種方法不推薦 API
level 16. 首選
findAllAsync(String) |
||||||||||
|
異步的查找網頁上所有的實例
|
||||||||||
|
Returns a View to enable grabbing screenshots from custom children returned in dumpViewHierarchyWithProperties.
|
||||||||||
|
滾動並加亮到下一個匹配的
findAllAsync(String) ,
直到網頁邊界 |
||||||||||
|
|
||||||||||
|
通知當前的WebView內存是低的,以便它可以釋放任何可用的內存
|
||||||||||
|
獲取主要頂層頁的SSL證書,或者null,如果沒有證書(該網站是不安全的)。
|
||||||||||
|
獲取的HTML內容的高度。
|
||||||||||
|
獲取當前頁的圖標。
|
||||||||||
|
Gets a HitTestResult based on the current cursor node.
|
||||||||||
|
對於一個給定的主機和域檢索HTTP身份驗證憑據.
|
||||||||||
|
獲取當前頁面的原始URL..
|
||||||||||
|
獲取當前頁面的進度.
|
||||||||||
|
不推薦使用此方法在API等級17。此方法很容易造成不準確的網頁渲染和UI線程之間的競爭。建議
onScaleChanged(WebView,
float, float) . |
||||||||||
|
獲取WebSettings的對象,用於控制設置此WebView.
|
||||||||||
|
獲取當前頁面的標題.
|
||||||||||
|
獲取當前頁面的URL.
|
||||||||||
|
返回到這個Web視圖的歷史記錄.
|
||||||||||
|
從當前的步驟數跳轉到歷史記錄的指定步驟數。.
|
||||||||||
|
WebView的歷史記錄前進
|
||||||||||
|
當前WebView調用圖形變焦選擇器.
|
||||||||||
|
獲取此WebView私人瀏覽是否已啓用.
|
||||||||||
|
這個WebView加載 由“data” 給定的數據
構建成的URL
|
||||||||||
|
Loads the given data into this WebView, using baseUrl as the base URL for the content.
|
||||||||||
|
加載此URL.
|
||||||||||
|
加載給定的URL指定額外的HTTP標頭.
|
||||||||||
|
這種方法被廢棄在API
level8。WebView不再需要viewgroup.onhierarchychangelistener。這種方法現在不會實現。
|
||||||||||
|
這種方法被廢棄在API
level8。WebView不再需要viewgroup.onhierarchychangelistener。這種方法現在不會實現。
|
||||||||||
|
創建一個新的InputConnection InputMethod與視圖交互
|
||||||||||
|
實現這個方法來處理通用的手勢事件
|
||||||||||
|
不推薦使用此方法 API level3。不應該執行WebView ViewTreeObserver.OnGlobalFocusChangeListener。這種方法現在什麼也不做。
|
||||||||||
|
實現這個方法來處理懸停事件.
|
||||||||||
|
|
||||||||||
|
初始化AccessibilityNodeInfo 關於視圖的信息. |
||||||||||
|
|
||||||||||
|
默認實現
KeyEvent.Callback.onKeyMultiple() :
總是返回false (不處理這個事件). |
||||||||||
|
|
||||||||||
|
暫停WebView和其相關聯的DOM,插件,JavaScript 等任何額外的處理
|
||||||||||
|
恢復webview在onPause()前後.
|
||||||||||
|
實現這個方法處理手勢事件。.
|
||||||||||
|
實現這個方法處理軌跡球運動事件.
|
||||||||||
|
當窗口被扣調用或者失去焦點.
|
||||||||||
|
獲取水平滾動條是否有疊加樣式
|
||||||||||
|
Gets whether vertical scrollbar has overlay style.
|
||||||||||
|
滾動WebView頁面大小一半內容
|
||||||||||
|
向上滾動WebView頁面大小一半內容
|
||||||||||
|
暫停webview 所有佈局,解析,javaScript times計時器
|
||||||||||
|
在視圖上執行指定的動作
|
||||||||||
|
如果被定義將調用調用view的OnLongClickListener
|
||||||||||
|
webview使用"Post"方法加載URL ,附帶postData 數據。
|
||||||||||
|
重新載入當前URL.
|
||||||||||
|
從這個web視圖中移除先前注入的Java對象.
|
||||||||||
|
當一組子類需要將一個特殊的矩形定爲到屏幕上.
|
||||||||||
|
調用該函數來嘗試給予指定的視圖或者其子類焦點和方向和特定矩形. 直到符合要求 by
getDescendantFocusability() . |
||||||||||
|
請求最後錨點或圖像元素的URL
|
||||||||||
|
最後一個接觸的用戶請求的URL的圖像
|
||||||||||
|
這從給定的Bundle對象恢復WebView的狀態,
|
||||||||||
|
恢復WebViews所有的佈局,解析和JavaScript計時器
|
||||||||||
|
設置爲指定的主機的用戶名和密碼.
|
||||||||||
|
|
||||||||||
|
保存當前視圖作爲Web存檔.
|
||||||||||
|
保存當前視圖作爲Web存檔
|
||||||||||
|
設置視圖的背景顏色
|
||||||||||
|
這種方法被廢棄APIlevel17。調用這個函數沒有有用的效果,並將在以後的版本中被忽略。.
|
||||||||||
|
當內容不能被渲染引擎處理和不應該下載時註冊一個接口來使用
|
||||||||||
|
在網頁上查找操作進展通知註冊監聽器
|
||||||||||
|
指定水平滾動條是否有疊加樣式
|
||||||||||
|
爲一個給定的主機和領域存儲HTTP身份驗證憑據
|
||||||||||
|
設置WebView初始比例
|
||||||||||
|
爲視圖的層指定類別. |
||||||||||
|
設置此視圖關聯的佈局參數
|
||||||||||
|
默認的情況下不推薦使用此方法 API level17。,實際將在未來的版本中支持.
|
||||||||||
|
通知WebView的網絡狀態
|
||||||||||
|
設置該視圖的滾動模式
|
||||||||||
|
不推薦使用此方法 API level12。這個方法現在已經過時。.
|
||||||||||
|
指定滾動條的樣式. |
||||||||||
|
指定垂直滾動條是否有疊加樣式.
|
||||||||||
|
設置腳本處理.
|
||||||||||
|
設置WebViewClient會接收到各種通知和請求
|
||||||||||
|
如果是按下的狀態應推遲到子類或其子類下返回true
|
||||||||||
|
在這個頁面開始查找文本
|
||||||||||
|
停止當前的加載.
|
||||||||||
|
執行放大該webView
|
||||||||||
|
執行縮小該webview
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
計算的水平滾動條上的滑塊水平範圍內的水平偏移量 |
||||||||||
|
計算水平滾動條的水平距離 |
||||||||||
|
計算的水平滾動條上的滑塊垂直範圍內的垂直距離 |
||||||||||
|
計算的垂直滾動條上的滑塊水平範圍內的垂直偏移 |
||||||||||
|
計算的垂直滾動條的垂直距離. |
||||||||||
|
當這個視圖連接一個窗口則調用.
|
||||||||||
|
目前正在使用的應用程序的資源配置改變時調用
|
||||||||||
|
當這個視圖脫離一個窗口則調用.
|
||||||||||
|
實現繪製
|
||||||||||
|
當系統的一個視圖的焦點狀態改變時調用
|
||||||||||
|
測量視圖和它的內容來確定測量寬度和測量高度. |
||||||||||
|
|
||||||||||
|
當此視圖內部內容滾動時回調此響應
|
||||||||||
|
當視圖的大小改變期間回調
|
||||||||||
|
當視圖可見狀態或視圖的父類可見狀態改變.
|
||||||||||
|
|
在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、獲取網站圖標
<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