Use of Android WebView

Source: Internet
Author: User

WebView is a subclass of view that can be used to embed H5 pages in the app and can be called with JS.

WebView has two methods: Setwebchromeclient and Setwebclient

Setwebclient: Mainly dealing with parsing, rendering web browsers and other things to do

Setwebchromeclient: Auxiliary webview handles JavaScript dialogs, website icons, site title and loading progress, etc.

Webviewclient is to help WebView handle various notifications, request events

When using WebView, you must set the Access network permissions in Androidmanifest.xml:

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

Control

    < WebView         Android:id = "@+id/webview"         android:layout_width= "Match_parent"        android:layout_height= "Match_ Parent "></WebView>

Use one: Load local/web resources

Using the WebView Loadurl () method

1. Load Local Resources

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

2. Load Web Resources

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

Use two: Open the Web page directly inside the app

Create a webviewclient, by Setwebviewclient Association

Webview.setwebviewclient (new  webviewclient () {    @Override    publicBoolean  shouldoverrideurlloading (WebView view, String URL) {        view.loadurl (URL);         return  true ;    }});

Use three: If there is JavaScript on the page visited, the WebView must be set to support JavaScript

// Enable JavaScript support WebSettings settings = webview.getsettings (); settings.setjavascriptenabled (true);

Use four: If you want to browse the Web page back not to exit the browser, you need to WebView overwrite URL loading, automatically generate historical access records

@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {    //Check If the key event is the back button and if there's history    if((keycode = = Android.view.KeyEvent.KEYCODE_BACK) &&Webview.cangoback ()) {        //return key BounceWebview.goback (); return true; }    //If It wasn ' t the back key or there's no Web page history, bubble up//To the default//system behavior (probably exit the activity)    return Super. OnKeyDown (KeyCode, event);}

Use five: Judge the page loading process

Webview.setwebchromeclient (new  webchromeclient () {    @Override    publicvoid  int  newprogress) {        //  TODO auto-generated method stub        if (newprogress = =) {            //  page loading complete          else  {             // Load in         }    }});

Purpose Six: Using caching

Prioritize using caching

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

Do not use cache

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

Other common settings for websetting:

Setjavascriptenabled (true);//Support JSSetpluginsenabled (true);//Support PluginsSetusewideviewport (false);//adjust the picture to fit the size of the WebViewSetsupportzoom (true);//Support ScalingSetlayoutalgorithm (Layoutalgorithm.single_column);//support for content re-layoutSupportmultiplewindows ();//Multi-WindowSetcachemode (websettings.load_cache_else_network);//Close cache in WebViewSetallowfileaccess (true);//settings can access filesSetneedinitialfocus (true);//set node for WebView when WebView calls RequestfocusWebView Websettings.setbuiltinzoomcontrols (true);//Setting Support ScalingSetjavascriptcanopenwindowsautomatically (true);//support to open a new window via JSSetloadwithoverviewmode (true);//Zoom to the size of the screenSetloadsimagesautomatically (true);//supports automatic loading of pictures

Use of Android WebView

Related Article

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.