Android: control webview Display Web Page-excerpt from Network

Source: Internet
Author: User
<span id="Label3"></p><p><p>WebView can make the Web page easy to embed into the app, but also directly with JS to call each other.</p></p><p><p>WebView has two methods: setwebchromeclient and setwebclient</p></p><p><p>Setwebclient: main processing parsing, rendering Web browsers and other things to do</p></p><p><p>setwebchromeclient: auxiliary webview Processing JavaScript Dialog box, website icon, website title, loading progress, etc.</p></p><p><p>Webviewclient is to help WebView handle various notifications, request Events.</p></p><p><p></p></p><p><p>To set access network permissions in Androidmanifest.xml:</p></p><pre><pre><uses-permission android:name= "android.permission.INTERNET"/></pre></pre><p><p>Control:</p></p><pre><pre><webview android:layout_width= "match_parent" android:layout_height= "match_parent" android:id= "@ +id/webview " /></pre></pre><p><p></p></p><p><p><strong>Use one: load Local/web resources</strong></p></p><p><p></p></p><p><p>Example.html stored in Assets folder</p></p><p><p>Call Webview's Loadurl () method,</p></p><p><p>Load Local Resources</p></p><pre><pre>WebView =<span style="color: #000000;"> (webView) findviewbyid (r.id.webview); webview.loadurl ("file:///android_asset/example.html");</span></pre></pre><p><p>To load a Web resource:</p></p><pre><pre>WebView =<span style="color: #000000;"> (webView) findviewbyid (r.id.webview); webview.loadurl ("http://baidu.com");</span></pre></pre><p><p></p></p><p><p><strong>Purpose two: Open the Web page within the program</strong></p></p><p><p><strong></strong></p></p><p><p></p></p><p><p><span style="line-height: 1.5;">Create a webviewclient of your own, by <span style="line-height: 1.5;">Setwebviewclient Association</span></span></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #0000ff;">Package<span style="color: #000000;">com.example.testopen;<span style="color: #0000ff;">Import<span style="color: #000000;">android.app.Activity;<span style="color: #0000ff;">Import<span style="color: #000000;">android.os.Bundle;<span style="color: #0000ff;">Import<span style="color: #000000;">android.webkit.WebView;<span style="color: #0000ff;">Import<span style="color: #000000;">android.webkit.WebViewClient;<span style="color: #0000ff;">Public<span style="color: #0000ff;">Class Mainactivity<span style="color: #0000ff;">Extends<span style="color: #000000;">Activity {<span style="color: #0000ff;">Private<span style="color: #000000;">WebView WebView; @Override<span style="color: #0000ff;">Protected<span style="color: #0000ff;">void<span style="color: #000000;">OnCreate (Bundle Savedinstancestate) {<span style="color: #0000ff;">Super<span style="color: #000000;">. onCreate (savedinstancestate); Setcontentview (r.layout.test); Init (); }<span style="color: #0000ff;">Private<span style="color: #0000ff;">void<span style="color: #000000;">init () {webView =<span style="color: #000000;">(WebView) Findviewbyid (r.id.webview);<span style="color: #008000;">//<span style="color: #008000;"><span style="color: #008000;">webview Load Web resource webview.loadurl ("http://baidu.com" <span style="color: #008000; >//<span style=" "); <span color: #008000; "> overwrite webview default use of Third-party or system default browser to open Web page behavior, so that the Web page is opened with WebView Webview.setwebviewclient (<span style="color: #0000ff;">new<span style="color: #000000;"> webviewclient () {@ Override <span style="color: #0000ff;">public <span style="color: #0000ff;">boolean <span style="color: #000000;" "> shouldoverrideurlloading (WebView view, String url) {<span style=" color: #008000; ">// <span style="color: #008000;"> TODO auto-generated method stub <span style="color: #008000;">//<span style="color: # 008000; "> The return value is true when control goes to WebView open, to false call system browser or Third-party browser <span style="color: #000000;"> View.loadurl (url); <span style="color : #0000ff; ">return <span style="color: #0000ff;">true<span style="color: #000000;" ;}}); } }< span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p><strong>Use Three:</strong></p></p><p><p><strong>If you have JavaScript on the page you are visiting, WebView must set up JavaScript support</strong></p></p><pre><pre><span style="color: #008000;"><span style="color: #008000;">Enable support javascriptwebsettings settings =<span style="color: #000000;"> webview.getsettings (); settings.setjavascriptenabled (<span style="color: #0000ff;">true);</span> </span> </span></span></pre></pre><p><p></p></p><p><p><strong>Use Four:</strong></p></p><p><p><strong>If you want to browse the page back instead of exiting the browser, you need to webview overwrite the URL to load, let it automatically generate historical access records, so that you can go forward or back to access the site has been visited.</strong></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><span style="color: #008000;">//<span style="color: #008000;">Rewrite physical Keys--return logic<span style="color: #000000;">@Override<span style="color: #0000ff;">Public<span style="color: #0000ff;">Boolean OnKeyDown (<span style="color: #0000ff;">Int<span style="color: #000000;"><span style="color: #000000;"> keycode, keyevent Event) {<span style="color: #008000;">//<span style="color: #008000;"> TODO auto-generated method stub <span style="color: # 0000ff; ">if (keycode==<span style="color: #000000;">keyevent.keycode_back) {<span style="color: #0000ff;">if<span style=" Color: #000000; "> (webview.cangoback ()) {webview.goback (); <span style="color: #008000;"> <span style="color: #008000;"> Return to previous page <span style="color: #0000ff;">return <span style="color: #0000ff;">true< Span style= "color: #000000;" >; } <span style="color: #0000ff;">else<span style="color: #000000;"> {system.exit (0); <span style="color: #008000;"> <span style="color: #008000;"> exit program <span style="color: #000000;">}} <span style="color: #0000ff;">return <span style="color: #0000ff;">super<span style="color: #000000;">.onkeydown (keycode, event);} </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><strong>Use Five: judge the page loading process</strong></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre> Webview.setwebchromeclient (<span style="color: #0000ff;">new<span style="color: #000000;"> webchromeclient () { @Override <span style="color: #0000ff;"><span style="color: #0000ff;"><span style="color: #0000ff;">int<span style="color: #000000;"><span style="color: #008000;">//<span style="color: #008000;"><span style="color: #0000ff;">if (newprogress = = +/<span style="color: #000000;"><span style="color: #008000;">/<span style="color: #008000;"> page load complete <span style="color: #000000;"><span style="color: #0000ff;">else<span style="color: #000000;"><span style="color: #008000;">//<span style="color: #008000;"> load <span style="color: #000000;">}});</span> </span> </span></span> </span></span> </span> </span></span> </span></span> </span></span> </span></span></span> </span> </span></pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><strong>Purpose Vi: use of the cache</strong></p></p><p><p>Prioritize using caching</p></p><pre><pre>Webview.getsettings (). Setcachemode (websettings.load_cache_else_network);</pre></pre><p><p>Do not use Cache:</p></p><pre><pre>Webview.getsettings (). Setcachemode (websettings.load_no_cache);</pre></pre><p><p></p></p><p><p></p></p><p><p>Related:</p></p><p><p>http://www.imooc.com/video/2269</p></p><p><p></p></p><p><p>Excerpt from Network: http://www.cnblogs.com/tinyphp/p/3858997.html</p></p><p><p>Android: control webview Display Web Page-excerpt from Network</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

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

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