There are two methods for JS extension in Android:
1. Use the JS extension interface provided by WebKit to expand at the Java layer, which is directly implemented by the app
Advantages: Easy
Disadvantage: coupling with the app. Other apps cannot use this JS extension.
2. Use npapi to expand on the CPP Layer
Advantage: All apps can share the extension.
Disadvantage: relatively difficult
The engine is mainly divided into three modules.
1. WebKit Platform Problems Code Is the platform port encapsulation for the following two modules
2. WebCore implements layout and rendering. jscore is used to process JavaScript scripts in HTML.
3. parse criptcore/V8 parse the JS script and execute
The interaction between jscore and WebCore is mainly related to binding. Map ing is available for data types. General JS extensions do not involve changes to jscore.
Use Android webview to call JavaScript Functions
Publicclass webviewdemo extends activity {private webview mwebview; private handler mhandler = new handler (); publicvoid oncreate (bundle icicle) {super. oncreate (icicle); setcontentview (R. layout. webviewdemo); mwebview = (webview) findviewbyid (R. id. webview); websettings = mwebview. getsettings (); websettings. setjavascriptenabled (true); // webview supports JavaScript mwebview. addjavascriptinterface (new object () {// Add the Javascript callable interface publicvoid clickonandroid () {mhandler. post (New runnable () {public void run () {mwebview. loadurl ("javascript: Wave ()") ;}}}, "Demo"); mwebview. loadurl ("file: // android_asset/demo.html"); // called webpage }}
Demo.html:
<HTML> <script language = "JavaScript"> function wave () {// prepare the document function called in the activity. getelementbyid ("droid "). src = "android_waving.png" ;}</SCRIPT>
<Body> <a onclick = "window. demo. clickonandroid () "> // call the clickonandroid () function of activity <br> click me! </A> </body>