How to use Android WebView

Source: Internet
Author: User

In the development process should pay attention to several points:
The license "Android.permission.INTERNET" must be used in 1.androidmanifest.xml, otherwise the Web page not available error will occur.
2. If there is JavaScript on the page visited, the WebView must be set to support JavaScript.
Webview.getsettings (). Setjavascriptenabled (True);

3. If the page links, if you want to click on the link to continue in the current browser response, instead of the new Android system browser should link in the ring, you must overwrite the WebView Webviewclient object.

Method/Step
  1. [Java] View plaincopyprint?
    Mwebview.setwebviewclient (New Webviewclient () {

    public boolean shouldoverrideurlloading (WebView view, String URL) {

    View.loadurl (URL);

    return true;

    }

    });

    Mwebview.setwebviewclient (New Webviewclient () {

    public boolean shouldoverrideurlloading (WebView view, String URL) {

    View.loadurl (URL);

    return true;

    }

    });

  2. 4. If you do not do any processing, browse the Web page, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback rather than launch the browser, you need to process and consume the back event in the current activity.


    [Java] View plaincopyprint?
    public boolean onKeyDown (int keycode, keyevent event) {

    if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {

    Mwebview.goback ();

    return true;

    }

    Return Super.onkeydown (KeyCode, event);

    }

    public boolean onKeyDown (int keycode, keyevent event) {

    if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {

    Mwebview.goback ();

    return true;

    }

    Return Super.onkeydown (KeyCode, event);

    }

  3. Next, let's look at how WebView in Android supports Javascripte custom objects, and JS has window,history,document standard objects in the standard. Similarly we can in the development of the browser when we define our own object call phone system function to handle, so use JS can do whatever you like.
    See an example:


    [Java] View plaincopyprint?
    View Plaincopy to Clipboardprint?

    public class Webviewdemo extends Activity {

    Private WebView Mwebview;

    Private Handler Mhandler = new Handler ();

    public void OnCreate (Bundle icicle) {

    Super.oncreate (Icicle);

    Setcontentview (R.layout.webviewdemo);

    Mwebview = (WebView) Findviewbyid (R.id.webview);

    WebSettings websettings = Mwebview.getsettings ();

    Websettings.setjavascriptenabled (TRUE);

    Mwebview.addjavascriptinterface (New Object () {

    public void Clickonandroid () {

    Mhandler.post (New Runnable () {

    public void Run () {

    Mwebview.loadurl ("Javascript:wave ()");

    }

    });

    }

    }, "demo");

    Mwebview.loadurl ("file:///android_asset/demo.html");

    }

    }

    public class Webviewdemo extends Activity {

    Private WebView Mwebview;

    Private Handler Mhandler = new Handler ();

    public void OnCreate (Bundle icicle) {

    Super.oncreate (Icicle);

    Setcontentview (R.layout.webviewdemo);

    Mwebview = (WebView) Findviewbyid (R.id.webview);

    WebSettings websettings = Mwebview.getsettings ();

    Websettings.setjavascriptenabled (TRUE);

    Mwebview.addjavascriptinterface (New Object () {

    public void Clickonandroid () {

    Mhandler.post (New Runnable () {

    public void Run () {

    Mwebview.loadurl ("Javascript:wave ()");

    }

    });

    }

    }, "demo");

    Mwebview.loadurl ("file:///android_asset/demo.html");

    }

    }

  4. Let's look at Addjavascriptinterface (object obj,string InterfaceName), a method that binds a Java object to a JavaScript object, and the JavaScript object name is InterfaceName (Demo), scope is global. Once the webview is initialized, the bound Java object can be accessed directly through Javascript:window.demo in the WebView loaded page. Let's see how it's called in HTML. Www.cdtarena.com

    [HTML] View Plaincopyprint?

    <mce:script language= "JavaScript" ><!--

    Function Wave () {

    document.getElementById ("Droid"). src= "Android_waving.png";

    }

    --></mce:script>

    <body>

    <a onclick= "window.demo.clickOnAndroid ()" >

    <br>

    Click me!

    </a>

    </body>

    <mce:script language= "JavaScript" ><!--

    Function Wave () {

    document.getElementById ("Droid"). src= "Android_waving.png";

    }

    --></mce:script>

    <body>

    <a onclick= "window.demo.clickOnAndroid ()" >

    <br>

    Click me!

    </a>

    </body>

    This makes it possible to invoke the Java object's Clickonandroid () method in JavaScript, and we can define a number of methods in this object (such as texting, calling the contact list, and so on-phone system functions. Here, the wave () method is an example of invoking JavaScript in Java.
    Here are a few more things to know:
    1) in order for WebView to load the Assets,android SDK from the APK file, a schema is provided with the prefix "file:///android_asset/". WebView encountered such a schema, go to the current package in the assets directory to find content. As in the above "file:///android_asset/demo.html"
    2) The Java objects and methods to be bound in the Addjavascriptinterface method are run in another thread and cannot be run in the thread that constructs them, which is also the purpose of using handler.

  5. Summarize:
    1, add permissions: Androidmanifest.xml must use the license "Android.permission.INTERNET", otherwise the Web page is not available error.
    2. Generate a WebView component in the activity: WebView WebView = new WebView (this);
    3, set WebView basic information:
    If you have JavaScript on the page you are visiting, WebView must set up support JavaScript.
    Webview.getsettings (). Setjavascriptenabled (True);
    Touch Focus Works
    Requestfocus ();
    Cancel scroll bar
    This.setscrollbarstyle (Scrollbars_outside_overlay);
    4. Set Wevview to display the page:
    Internet use: Webview.loadurl ("http://www.google.com");
    For local files: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
    5. If you want to click on the link handled by yourself, instead of the new Android system browser should link.
    Add an Event Listener object (webviewclient) to WebView
    And rewrite some of these methods
    Shouldoverrideurlloading: The response to a hyperlink button in a Web page.
    Webviewclient calls this method when a connection is pressed and passes a parameter: The URL that is pressed
    Onloadresource
    Onpagestart
    Onpagefinish
    Onreceiveerror
    Onreceivedhttpauthrequest
    6, if the WebView point link to see a lot of pages later, if you do not do any processing, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback instead of exiting the browser, The back event needs to be processed and consumed in the current activity.
    Overrides the onkeydown (int keycoder,keyevent event) method of the Activity class.

    [Java] View plaincopyprint?
    public boolean onKeyDown (int keycoder,keyevent event) {

    if (Webview.cangoback () && Keycoder = = keyevent.keycode_back) {

    Webview.goback (); GoBack () indicates that the previous page of the WebView is returned

    return true;

    }

    return false;

    }


How to use Android WebView

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.