Calls in JavaScript in Android

Source: Internet
Author: User

Refer to Zhang Yang: 2048-ai program algorithm analysis, and analysis of the Android version of the source code, think that Android calls JavaScript code can be quickly re-use of web codes, improve development efficiency, then the Android and JavaScript call each other to learn and record.

First, what is JS and Android interaction?

The popular point is to use the JS code to call Java code, or use Java code to invoke the JS code. That is, mixed programming.

Second, why use JS to interact with Java code?

1. You can do some JS Web pages do not handle the things themselves. For example, call Android locally on a webpage, send SMS, call the Android phone address Book on a webpage, call a third-party app on a webpage, call some Android localized processing on a webpage (Operation IO, database), etc.

2. JS developers can be implemented in collaboration with Android developers. For example: JS developers to develop JS code for Android developers to call, Android developers do not have to write JS code, you can focus on Andrid development, use the JS when the direct call JS code can be. (and vice versa)

3. The dynamic update of the webpage can be realized. For example, you can use the JS code to call the Java Update data displayed on the Web page to achieve the purpose of dynamically updating the page.

Third, the Android and JS code calls each other

1.Android Terminal code

  

 Public classMyActivityextendsActivity {PrivateWebView Mywebview; PrivateImageButton IButton; Private BooleanFlag =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.activity_my); IButton=(ImageButton) Findviewbyid (R.id.imagebutton); Mywebview=NewWebView ( This); Mywebview.getsettings (). setjavascriptenabled (true); //Enable JavaScript Mywebview.addjavascriptinterface ( This, "Android"); //Bind JavaScript with Android code Mywebview.loadurl ("File:///android_asset/index.html");//load Web, that is, JS portal. JS Call Android //ibutton.setimagedrawable (Getresources (). getdrawable (R.drawable.eg_bulbon));} @JavascriptInterface // if Target is greater than or equal to API 17, you need to add the following annotations
Public voidOnjsrun (Booleanflag) { if(flag) {LOG.E ("Ai~~~~~", "true"); Handler.sendemptymessage (1); }Else{LOG.E ("Ai~~~~~~~", "false"); Handler.sendemptymessage (2); } } PublicHandler Handler =NewHandler () { Public voidhandlemessage (Message msg) {Switch(msg.what) { Case1: LOG.E ("111111111", "iscalled"); Ibutton.setbackground (Getresources (). getdrawable (R.drawable.eg_bulbon)); Try{thread.currentthread (); Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); } mywebview.loadurl ("Javascript:changeimage (False)"); //android Direct JS function Break; Case2: LOG.E ("2222222222222", "iscalled"); Ibutton.setbackground (Getresources (). getdrawable (R.drawable.eg_bulboff)); Try{thread.currentthread (); Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); } mywebview.loadurl ("Javascript:changeimage (True)"); Break; default: Break; } } }; @JavascriptInterface // if Target is greater than or equal to API 17, you need to add the following annotations

Public voidDebug (String msg) {LOG.E ("AI", MSG); }

Web-side code:

<! DOCTYPE html> Changeimage (true); </script><p> tap the light bulb to light or turn off this lamp. </p></body>

JS Code

function changeimage (flag) {Android.debug ("The JS call is run"//js calls the Android function android.onjsrun (flag);}

Operation Result: 300ms,ui change.

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.