Two Methods for Phonegap to access the local interface through JS

Source: Internet
Author: User

Phonegap development documentation

Java code
  1. <Span style = "font-size: 14px;"> package org. apache. cordova. plugin;
  2. Import org. apache. cordova. api. CordovaPlugin;
  3. Import org. apache. cordova. api. PluginResult;
  4. Import org. json. JSONArray;
  5. Import org. json. JSONException;
  6. Import org. json. JSONObject;
  7. /**
  8. * This class echoes a string called from JavaScript.
  9. */
  10. Public class Echo extends CordovaPlugin {
  11. @ Override
  12. Public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  13. If (action. equals ("echo ")){
  14. String message = args. getString (0 );
  15. This. echo (message, callbackContext );
  16. Return true;
  17. }
  18. Return false;
  19. }
  20. Private void echo (String message, CallbackContext callbackContext ){
  21. If (message! = Null & message. length ()> 0 ){
  22. CallbackContext. success (message );
  23. } Else {
  24. CallbackContext. error ("Expected one non-empty string argument .");
  25. }
  26. }
  27. } </Span>

Xml Code
  1. <Span style = "font-size: 14px;"> <plugin name = "Echo" value = "org. apache. cordova. plugin. Echo"/> </span>

    Js Code
    1. <Span style = "font-size: 14px;"> window. echo = function (str, callback) {</span>
    2. <Span style = "font-size: 14px;"> <strong> var exec = cordova. require ('cordova/exec '); </strong>
    3. <Strong> exec </strong> (callback, function (err ){
    4. Callback ('Nothing to echo .');
    5. }, "Echo", "echo", [str]);
    6. }; </Span>

       

      Js Code
      1. <Span style = "font-size: 14px;"> window. echo ("echome", function (echoValue ){
      2. Alert (echoValue = "echome"); // shocould alert true.
      3. }); </Span>

      Java code
      1. <Span style = "font-size: 14px;"> cordova. getActivity (). runOnUiThread (new Runnable (){
      2. Public void run (){
      3. ...
      4. CallbackContext. success (); // Thread-safe.
      5. }
      6. });
      7. </Span>

      Java code
        1. <Span style = "font-size: 14px;"> cordova.getthreadpool(.exe cute (new Runnable (){
        2. Public void run (){
        3. ...
        4. CallbackContext. success (); // Thread-safe.
        5. }
        6. }); </Span>

      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.