Android WebView Use summary _android

Source: Internet
Author: User

This example for you to share the Android WebView use summary for your reference, the specific contents are as follows

# using overloaded URL to realize Java and JS interaction

In Android, the common Java and JS interaction is implemented through function Addjavascriptinterface to add the callback proxy class used in JS.
Although this method is convenient, but the written JS code is not universal. If iOS also wants to implement similar functionality or business, iOS will have to write another set of JS code. So it's not recommended.

It is recommended to use overloaded URLs, because basically all platforms have callback functions that do some processing before a URL is loaded. So this approach will be more common.

# Responding to alert in JavaScript

In the Android WebView control, the default alert function for JS is unresponsive.
To eject the corresponding dialog box, we need to implement it ourselves.
The specific implementation code is as follows:

Webview.getsettings (). Setjavascriptenabled (true);
Webview.setwebchromeclient (New Webchromeclient () {
  @Override public
  boolean Onjsalert (WebView view, String URL, String message, final Jsresult result) {
    Alertdialog.builder Builder = new Alertdialog.builder ( Mainactivity.this);
    Builder.settitle ("Test Alert");
    Builder.setmessage (message);
    Builder.setpositivebutton (OK), new Onclicklistener () {
      @Override public
      void OnClick (Dialoginterface dialog , int which) {
        result.confirm ();
      }
    });
    Builder.show ();
    return true;
  }
});

Through the above code can be implemented when the JS in the implementation of alert, on Android on the original dialog box display, of course, this can also be directly toast.

And there are two points to note in this code:

1, the return value must be true.
returns True, the description has been processed and does not need to be performed by Webchromeclient. If the return is false, then WebView will continue to execute the subsequent JS code, the phenomenon is that the pop-up dialog box, the user has not been identified, the subsequent JS code has been executed.

2, finally must call Result.confirm ().
The reason is that if this function is not called, subsequent JS code will not continue to execute. The most common phenomenon is that the Alert dialog box appears only once, and there is no response to the second alert. In fact, this is because the Confirm function is not invoked, which is equivalent to the user having no point of certainty after alert in the browser.

3, result.confirm () should be placed in the onclick callback.
as mentioned earlier, calling the Confirm function is equivalent to the user clicking on the OK button. So we're going to put the call to the confirm function in the callback function of the "OK" button in the Android Native dialog box.
I didn't understand it before, Put the Confirm function in the builder.show after the call, the result of the phenomenon is alert, the dialog box bounced out, but the subsequent JS code is not blocked, but continue to carry on, become like asynchronous, and in the Chrome Call JS code execution logic is inconsistent.
However, if the demand is to be carried out directly, it can also, as long as the understanding of the line.

# Access Certificate has a problem SSL Web page

For pages with a certificate problem, such as expiration, incorrect information, and not being trusted by the issuing authority, WebView will deny access by default. and PC-side browser processing is to provide users to choose whether to continue, in Android can also be achieved.
The first is to go straight ahead without having to let the user choose

@Override public
void Onreceivedsslerror (WebView view, Sslerrorhandler handler, sslerror error) {
 // The default is to invoke the Handler.cancel () method, so do not call the Super Onreceivedsslerror method
  //Super.onreceivedsslerror (view, handler, error);
 Handler.proceed ();
}

Note here that you should never call the Onreceivedsslerror method of Super, because Handler.cancel () is already called in this method.
If invoked, the first failure to load and the second normal access occurs.

# Be careful to return true in Shouldoverrideurlloading

When Webviewclient is set, in the shouldoverrideurlloading, if you do not need to intercept the URL to do processing, but simply continue to load this URL.
It is recommended that the URL be loaded in a way that returns false rather than Loadurl.

Why do you suggest that?
Because if the loadurl way to load, then for the load has a jump URL, Webview.goback will be particularly troublesome.
For example, the load link is as follows:
a-> (b->c->d)->e in parentheses for jumps
If you use return false, you can go back to page a from the second step when GoBack. From e back to a you only need to perform two times GoBack
And if the use of Loadurl, there is no way directly from the second step back to page A. Because Loadurl the second step of each jump is considered a new page load, so from e back to a need to perform four times GoBack

You should return true only if you do not need to load URLs to intercept and do other processing, such as intercepting tel:xxx and other special URLs for dial-up processing.

# onformresubmission

Original link:http://zlv.me/posts/2015/01/14/08_Android-Webview use summary/

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.