Android: Control webview Display Web page

Source: Internet
Author: User

WebView can make the Web page easy to embed into the app, but also directly with JS to call each other.

WebView has two methods: Setwebchromeclient and Setwebclient

Setwebclient: Main processing parsing, rendering Web browsers and other things to do

Setwebchromeclient: Auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress, etc.

Webviewclient is to help WebView handle various notifications, request events.

To set access network permissions in Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/>

Control:

<webview     android:layout_width= "match_parent"    android:layout_height= "match_parent"    android:id= "@ +id/webview "    />

Use one: Load local/web resources

Example.html stored in Assets folder

Call WebView's Loadurl () method,

Load Local Resources

WebView = (WebView) Findviewbyid (R.id.webview); Webview.loadurl ("file:///android_asset/example.html");

To load a Web resource:

WebView = (WebView) Findviewbyid (R.id.webview); Webview.loadurl ("http://baidu.com");

Purpose Two: Open the Web page within the program

Create a webviewclient of your own, by Setwebviewclient Association

PackageCom.example.testopen;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.webkit.WebView;ImportAndroid.webkit.WebViewClient;PublicClass MainactivityExtendsActivity {PrivateWebView WebView; @OverrideProtectedvoidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.test); Init (); }PrivatevoidInit () {WebView =(WebView) Findviewbyid (R.id.webview);//webview load Web Resource webview.loadurl ("http://baidu.com"  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 (new Webviewclient () {@ Override public boolean  Shouldoverrideurlloading (WebView view, String URL) {//  TODO auto-generated method stub // The return value is true when control goes to WebView open, to false call system browser or third-party browser  View.loadurl (URL); return true 

Use three:

If you have JavaScript on the page you are visiting, WebView must set up JavaScript support

Enable support javascriptwebsettings settings = webview.getsettings (); settings.setjavascriptenabled (true);  

Use four:

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.

//Rewrite physical keys--return logic@OverridePublicBoolean OnKeyDown (Int KeyCode, KeyEvent event) {// TODO auto-generated method stub if (Keycode==keyevent.keycode_back) {if (Webview.cangoback ()) {webview.goback ();   Return to previous page return true< Span style= "color: #000000;" >; } else {system.exit (0);   exit program }} return super.onkeydown (KeyCode, event);}         

Use five: Judge the page loading process

    Webview.setwebchromeclient (new webchromeclient () {            @Override            int//if (newprogress = = +// page load complete else// load }});          

Purpose VI: Use of the cache

Prioritize using caching

Webview.getsettings (). Setcachemode (Websettings.load_cache_else_network);

Do not use cache:

Webview.getsettings (). Setcachemode (Websettings.load_no_cache);

Related:

http://www.imooc.com/video/2269

Android: Control webview Display Web page

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.