Android native and Hml interaction (webview Base)

Source: Internet
Author: User

    • WebView Loading page

There are two ways to load WebView,

Load Network Address

Webview.loadurl ("www.xxx.com/index.html");

Load Local Resources

Webview.loadurl ("file:///android_asset/example.html");
    • Add request header information, where you can add authentication information, etc.
map<string,string> map=New hashmap<string,string>() map.put ("Taken", "1234"); Webview.loadurl ("Http://192.168.99.101:8080/xiaoyanAPI/test1", map);
    • Set the link in the page to jump, intercept the jump event, in the corresponding function to do the corresponding processing.
Webview.setwebviewclient (new  webviewclient () {            publicboolean  Shouldoverrideurlloading (WebView view, String URL) {                view.loadurl (URL);                 return true ;            }        });
    • Set Allow JS method to execute
        WebSettings websettings=webview.getsettings ();        Websettings.setjavascriptenabled (true);
    • The return key returns to the previous page (the default WebView the page into the cache stack)
@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        if(KeyCode = =keyevent.keycode_back) {            if(Webview.cangoback ()) {webview.goback ();//go back to previous browse page                return true; } Else{finish ();//Close Activity            }        }        return Super. OnKeyDown (KeyCode, event); }
    • The JS method of calling HTML on Android

Call the no return value method to remove the back of the function, which accepts the data returned by the JS function.

New Valuecallback<string>() {        @Override        publicvoid  Onreceivevalue ( String value) {            "onreceivevalue value=" + value);        }    });
    • JS calls the original method of Android.

Above Android4.2 can be declared directly using @javascriptinterface annotations, the following is a local Java method

 Public class jsinteration {    @JavascriptInterface    public  String back () {        return " Hello world ";    }}

After you define this method, call the Mwebview.addjavascriptinterface () method:

Mwebview.addjavascriptinterface (new jsinteration (), "Android");

So how to invoke in JS?

<script type= "Text/javascript" >     function S () {     // call Java Back () method    var result =Window.android.back ();    document.getElementById ("P"). innerhtml=result;    } </script>

Android native and Hml interaction (webview Base)

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.