The following example shows how to share your experience with a friend on the Internet. I posted this article, so you should pay more attention and make fewer mistakes.
Important:
Key code in android:
Webview. getSettings (). setJavaScriptEnabled (true );
Webview. addJavascriptInterface (object, "name"); // Add the object Name = "name" to the object. If the object is this, It is window. name.
Webview. loadUrl ("file: // android_asset/index.html"); // note that the resource is located in the same folder as res.
Call the method code in android in js or html:
For example:
Or the method () in window. name. java in js ();
The function Method of calling js in android:
Callfunction (){
Webview. loadUrl ("javascript: function ()");
}
Note that many data types are not recognized in js. It is best to encapsulate them in android to provide necessary method interfaces. For example, if you upload the list to js, you cannot obtain the elements in js.
AddJavascriptInterface: the Java object and method to be bound in the addJavascriptInterface method must run in another thread, and cannot run in the Construction thread, that is, cannot run in the current activity thread, is to bind this method to the page, js can also call
Explanation in this document:
Use this function to bind an object to Javascript so that the methods can be accessed from Javascript.
The Java object that is bound runs in another thread and not in the thread that it was constructed in.
In fact, the above are all nonsense:
Important: The following two connections
All of them are for reference:
1:. http://ysongren.blog.163.com/blog/static/6154220720109211020410/
2. http://blog.csdn.net/hanyuwei0/article/details/6541531