JS extension and interaction in Android

Source: Internet
Author: User


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> 

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.