1. #1
    Sencha Premium Member IAmCoder's Avatar
    Join Date
    Mar 2012
    Posts
    33
    Vote Rating
    1
    IAmCoder is on a distinguished road

      1  

    Default Calling Native Code in Android from JavaScript using Sencha Touch and PhoneGap 2.2.0

    Calling Native Code in Android from JavaScript using Sencha Touch and PhoneGap 2.2.0


    Here is a simple example of how to call native code in Android from within a Sencha Touch application, stripped down to the core elements: http://www.btek.com.au/calling-nativ...om-javascript/.

    It took me way longer than it should because I kept getting the “Class not found” error when I pressed the button, which means that something went wrong when trying to load the native class referenced in \res\xml\config.xml. I had it failing because there was no icon in \res\drawable\, believe it or not. And at some point I had an outdated config.xml in the \bin\ and \gen\ directories.

    \assets\www\index.html
    Code:
    <html>
     <head>
      <title>BTek PhoneGap Plugin</title>
      <link rel="stylesheet" type="text/css" href="sencha-touch.css"/>
      <script type="text/javascript" src="sencha-touch-all.js"></script>
      <script type="text/javascript" src="cordova-2.2.0.js"></script> 
      <script type="text/javascript" src="app.js"></script>
      <script type="text/javascript">
    
        function CallNative() {
            cordova.exec(SuccessHandler, ErrorHandler, 'BTekPlugin', '', []);
        }
                
        function SuccessHandler (result) { 
              Ext.Msg.alert(result.title, result.details); 
        }
             
        function ErrorHandler (error) { 
               alert("Native call failed: " + error);
        } 
      </script>
     </head>
     <body></body>
    </html>
    \res\xml\config.xml
    Code:
     <plugin name="BTekPlugin" value="au.com.BTek.Plugins.BTekPlugin"/>
    \src\au\com\BTek\Plugins\BTekPlugin.java
    Code:
    package au.com.BTek.Plugins;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.apache.cordova.api.Plugin;
    import org.apache.cordova.api.PluginResult;
    
    
    public class BTekPlugin extends Plugin {
    
    
      public BTekPlugin() {
      }
    
    
      public PluginResult execute(String action, JSONArray args, String callbackId)
      {
        // Create the response JSON
        JSONObject jsonResponse = new JSONObject();
        try
        {
          jsonResponse.put("title", "Hello JavaScript!");
          jsonResponse.put("details", "How are you today?");
        }
        catch(JSONException e)
        {
          // Raise the error event
          this.error(e.getMessage(), callbackId);
        }
    
    
        // Raise the success event
        this.success(new PluginResult(PluginResult.Status.OK, jsonResponse), callbackId);
    
    
        return new PluginResult(PluginResult.Status.OK, "success");
      }
    }
    PhoneGap Plugin.png

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    St. Louis, MO
    Posts
    33,656
    Vote Rating
    435
    mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of mitchellsimoens has much to be proud of

      0  

    Default


    Thanks for sharing!
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    http://www.JSONPLint.com - Source to lint your JSONP!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is almost in print!

    When posting code, please use BBCode's CODE tags.