Detailed Android JS calls each other _android

Source: Internet
Author: User
Tags uuid

Recently in the study of Android, JS call each other, not how to contact before, only know Loadurl () can load a Web page, after the study found that Android can tune Js,js can also adjust the original Android control, very happy AH. The following small series will begin:
The principle is Java and JS calls, in Android is through the webview to achieve.
Let's talk about simple Android and JS calling each other

    • First load Web pages with Loadurl ()
    • WebView Open JS Script execution
    • An interactive interface that provides JS calls on the Android side

Take a quick look at the code:

 mwebview= (Mywebview) Findviewbyid (r.id.wv_container_information);
    mwebsetting = Mwebview.getsettings ();
Mwebsetting.setjavascriptenabled (TRUE);
   JS calls the method in Android Mwebview.addjavascriptinterface (this, "XXX");//xxx method in HTML/** * Client provides send_comment method is called by JS
  * By:chenhe AT:2015/09/28 * * @param uuid UUID * @param fun_name called method name * @param JSON JS to the client JSON * * * @JavascriptInterface public void send_comment (final string uuid, final string fun_name, final string json) {new T Hread (New Runnable () {@Override public void run () {commend = Commandfactory.getinstance (). Createcomm
        and (Fun_name);
          if (commend!= null) {commend.setfun_name (fun_name);
          Commend.setmhandler (Mhandler);
          Commend.setparam (JSON);
          Commend.setuuid (UUID);
          Commend.setwebview (Mwebview);
        Commend.do_result (context);
  }}). Start (); }

This is done by Mwebview.addjavascriptinterface () to call the method in JS, that is, the second parameter is the method in the Web page called XXX (), and the parameters inside it are send_command below me () ; The parameters in the method then I'll do some action. Finally through the Loadurl (), the implementation is invoked. Very simple to believe that everyone will also use, also used, then give us a detailed description of how through such a call to achieve JS calls to the Android side of some controls .

Steps

    • First JS calls a customer service end of a method, the required parameters passed over
    • Then the client according to the requirements of JS to perform related operations
    • Back out of the JS method after performing the operation

Concrete implementation

The first step is the process described above

Here omitted (where Fun_name is JS to adjust the method name for example: for example, it wants the client to implement pop-up dialog, then the Web side to define a method named: Popup.alert)

Customer service by means of the name to perform the relevant operations

Through the method name we go to the local pop-up dialog, or based on the JSON data returned by the server to perform some other complex operations, I do not specify here, can have a lot of, such as open the Address Book to select Contacts, get the phone unique identifier, set navigation bar, pop-up date selection box, and so on

After performing the relevant operations and then callback JS method to return the information to JS

such as the above open Address Book Select Contact, we can return the contact's mobile phone number and other attributes via JSON to JS.
The general idea is this, look at the code:

@JavascriptInterface public void Send_comment (final string fun_name, final string json) {//According to Fun_name processing different business new Th
      Read (new Runnable () {@Override public void run () {//Note that this must be an asynchronous processing jsonobject jsonobject = null;
      try {jsonobject = new jsonobject (JSON);
      catch (Jsonexception e) {e.printstacktrace ();
      String title = jsonobject.optstring ("title");
      String message = jsonobject.optstring (' message ');
      TODO: Perform related operations based on message msg = new Message ();
      Msg.what = 0;
      Msg.obj = Jsonobject;
    Handler.senmessage (msg);
}). Start (); /** * Callback JS method will process information back to JS * @param issuccess * @param json/public void Returntojs (final boolean issuccess, Final JS
      Onobject json) {LOG.D ("----", "javascript:app_result (' + issuccess +" ', ' "+ json.tostring () +") ");
      String data = "Javascript:app_result (' + issuccess +" ', ' "+ json.tostring () +") ";
Webview.loadurl (data); Private HandleR handler = new Handler () {@Override public void handlemessage (message msg) {super.handlemessage (msg);
    if (msg.what==0) {//todo: Perform related Operations Returntojs (true, (Jsonobject) msg.obj);

 }
  }
};

This realizes the basic JS to adjust the original control of the Android, mainly with JS with good can be perfectly realized, now a lot of apps are used in this way to achieve. So to understand this piece is necessary, in fact, nothing, said JS call Android, in fact, Android here to provide a Java interface to Loadurl () can be.

The above is for you to share the Android JS each other specific methods of invocation, I hope to help you learn.

Related Article

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.