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 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
interface | WebView.FindListener | 监听查找结果的接口 | |||||||||
class | WebView.HitTestResult | ||||||||||
interface | WebView.PictureListener | 不推荐使用此接口在API级别12。该接口是现在已经过时。 | |||||||||
class | WebView.WebViewTransport | 返回的WebView跨线程边界传输对象 |
[Expand]
|
---|
[Expand]
继承XML 属性
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
常量 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
String | SCHEME_GEO | URI scheme for map address. | |||||||||
String | SCHEME_MAILTO | URI scheme for email address. | |||||||||
String | SCHEME_TEL | URI scheme for telephone number. |
继承的常量
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() | |||||||||||
![]() |
[展开]
继承的字段
| |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
公共构造函数 | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
的WebView(
上下文上下文)
一个Context对象构造一个新的WebView。
| |||||||||||
的WebView(
上下文背景下
的AttributeSet ATTRS)
构造一个新的WebView与布局参数。
| |||||||||||
的WebView(
上下文背景下
的AttributeSet ATTRS,INT defStyle)
构造一个新的WebView与布局参数和默认样式。
| |||||||||||
的WebView(
上下文背景下
的AttributeSet ATTRS,INT defStyle,布尔privateBrowsing)
这个构造函数在API等级17是不推荐。私人浏览功能不再支持直接通过WebView,将在未来的版本中删除。不再私自访问 WebSettings ,而改用WebViewDatabase ,CookieManager 和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() | ||||||||||
void |
clearSslPreferences()
清除
存储
响应与
SSL
证书错误.
| ||||||||||
void |
clearView()
清除此
Web视图
,
如果
measurespec
不
measurespec.exactly
。
ondraw()
将
只有白色的
背景
,
并
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<
String,
String> additionalHttpHeaders)
加载给定的URL指定额外的HTTP标头.
| ||||||||||
void |
onChildViewAdded(
View parent,
View child)
这种方法被废弃在API level8。WebView不再需要viewgroup.onhierarchychangelistener。这种方法
现在
不会实现
。
| ||||||||||
void |
onChildViewRemoved(
View p,
View child)
这种方法被废弃在API level8。WebView不再需要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) | ||||||||||
void |
onInitializeAccessibilityNodeInfo(
AccessibilityNodeInfo info)
初始化AccessibilityNodeInfo 关于视图的信息.
| ||||||||||
boolean | onKeyDown(int keyCode, KeyEvent event) | ||||||||||
boolean |
onKeyMultiple(int keyCode, int repeatCount,
KeyEvent event)
默认实现
KeyEvent.Callback.onKeyMultiple() : 总是返回false (不处理这个事件).
| ||||||||||
boolean | onKeyUp(int keyCode, KeyEvent event) | ||||||||||
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)
调用该函数来尝试给予指定的视图或者其子类焦点和方向和特定矩形. 直到符合要求 by
getDescendantFocusability() .
| ||||||||||
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) | ||||||||||
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) | ||||||||||
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)
|
在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.youkuaiyun.com/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.youkuaiyun.com/qeqeqe236/article/details/7992043