Android Embedded HTML5 and interact

Source: Internet
Author: User

The interaction between Android and HTML5 is primarily two parts, interaction with HTML5, and interaction with JavaScript, and interaction with HTML5 can be translated by registering the OnClick event into the interaction with JavaScript

The interaction between Android and JavaScript is primarily achieved by invoking methods, which Android can call a call to JavaScript, but JavaScript calls to Android code are intercepted (@ Javascriptinterface and mapping implementations have many vulnerabilities and are not recommended)

Android calls JavaScript code in two ways, through the WebView object's Loadurl (string) or Evaluatejavascript (string, Valuecallback

// audioPrepareResult是当前页面中的一个JavaScript方法// startRecordResult是Android本地方法返回的结果, 作为参数传递给JavaScript方法mWebView.evaluateJavascript("javascript:audioPrepareResult(\"" + startRecordResult + "\")", new ValueCallback<String>() {    @Override    public void onReceiveValue(String value) {        // nothing    }});

Here is a hole, if the parameters of the JavaScript method is a string, you must manually add "(quotation marks) before and after the parameters, otherwise JavaScript will produce undefined, compared to the wonderful thing is, if the parameter is a JSON, so there is" {\ " Longitude\ ": 0, \" latitude\ ": 0}" This code

There are three JavaScript calls to Android, the first of which is mapped by WebView's Addjavascriptinterface (Object, String), which is handy, but it is not recommended for serious vulnerabilities. The second method is to block shouldoverrideurlloading (WebView, String) for URL interception, matching the contract with the corresponding method call. The third is by rewriting the Onjsalert in the Webchromeclient (WebView view, string URL, String message, string defaultvalue, jspromptresult result ), onjsconfirm (/* parameter with Onjsalert/), onjsprompt (/ parameter with onjsalert*/) to intercept the corresponding dialog box, matching the message of parameter 3, Match with the contract to execute the corresponding Android code, the following gives a third code demo:

mWebView.setWebChromeClient(new WebChromeClient() {     @Override    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {        Uri uri = Uri.parse(message);     if (uri.getScheme().equals("CallAndroidMethod")) {        switch (uri.getAuthority()) {            case "recordAudio":                boolean recordAudioResult = MainActivity.this.recordAudio();                result.confirm(new Boolean(recordAudioResult).toString());                return true;            default:                break;        }    }    result.confirm(new Boolean(true).toString());    return super.onJsPrompt(view, url, message, defaultValue, result);    }});

Calling this code corresponds to the H5 code:

There's a pit in onjsprompt (WebView view, string URL, String message, string defaultvalue, jspromptresult result) The Confirm (bool) method of the last parameter Jspromptresult must be called before the method returns, otherwise it will cause the HTML5 page to be unresponsive

Some other actions:
WebView's CanGoBack () and GoBack () methods can implement the activity's onkeydown (int keycode, keyevent event) to enable the user to press the return key to return to the previous Web page rather than exit the page

Android Embedded HTML5 and interact

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.