Android WebView using tips with some pits _android

Source: Internet
Author: User
Tags documentation

With the performance of the mobile phone, as well as the popularity of iOS and Android two platforms, more apps are going to be developed in the two-platform app, and in some cases, for faster development, we'll use hybird to develop, This time we need to use WebView and make some configuration ourselves. Android's WebView has a different webkit version of the kernel in both the lower and the newer versions, using Chrome directly after 4.4, so there's a lot of problems here, sharing some of the tricks I've used and the pits I've encountered.
# # #webview配置 ###

Mwebview.getsettings (). Setjavascriptenabled (True); Settings allow JavaScript
//HTML5 API flags
mwebview.getsettings (). Setappcacheenabled (true);//set Allow caching
Mwebview.getsettings (). Setdatabaseenabled (True); Set allowed to use Localstore

The above webview.getsettings () Gets the WebSettings object, where some of the webview settings are saved, such as those set above, and more settings see the WebSettings API documentation.
Usually we also use the webviewclient and Webchromeclient components to assist the webview. Webviewclient mainly helps to handle various notification request events, such as page start loading, load completion, etc. Webchromeclient main Auxiliary webview Processing JavaScript dialog box, site icon, site title, load progress, and so on.
These two components should actually be used in accordance with the actual situation, and the response method should be overridden to perform some of its own operations.
# # #Javascript的使用 ###
the way to open JavaScript has been mentioned above.
The client invokes the JS code in the Web page, or executes the appropriate code.

private void Evaluatejavascript (String js) { 
 if Build.VERSION.SDK_INT >= build.version_codes. KitKat) {
  mwebview.evaluatejavascript (JS, null);
 } else {
  mwebview.loadurl (JS);
 }
}

The android4.4 start system provides a Evaluatejavascript method to execute the JS method and can be recalled. But in less than 4.4 version does not have this method, we need as long as directly through the Loadurl way to execute JS, at this time need to add "javascript:" Before the JS code.
You can also define some JavaScript in the client to call in the Web page.
Like this:
First, define a class to be executed for JS:

public class Webappinterface {context
 mcontext;

 /** instantiate the interface and set the context * *
 webappinterface (context c) {
  mcontext = c;
 }

 /** show a toast from the Web page *
 /@JavascriptInterface public
 void Showtoast (String toast) {
  Toast.make Text (Mcontext, Toast, Toast.length_short). Show ();
 }

Webview.addjavascriptinterface (This), "Android" (new Webappinterface);

After using *addjavascriptinterface& set to WebView, in JS can be used android.showtoast ("FDF") called.
Note that the interface method we give JS needs to be public, using the javascriptinterface annotation, this annotation is added at Android4.2 time, and the updated Android is not available if this annotation is not added.
# # #硬件加速 ###
Hardware acceleration is a big hole, please do not open it.
The chrome that is used after android4.4, the system will open itself.
# # #其他 ###
and the use of webview, to forget to apply for network access to apply permissions.
There are also some knowledge points have not been sorted, please refer to the WebView documentation, more pits later step on the update.
In addition Jeremyhe summed up the knowledge is also good, can refer to: http://www.jb51.net/article/86956.htm

Original address: http://blog.isming.me/2015/10/18/webview-use/

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.