Results 1 to 2 of 2

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

  1. #1
    Sencha Premium Member IAmCoder's Avatar
    Join Date
    Mar 2012
    Vote Rating

    Default 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:

    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.

      <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);
     <plugin name="BTekPlugin" value=""/>
    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();
          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 - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Gainesville, FL
    Vote Rating


    Thanks for sharing!
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    Learn BBCode and use it!

    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2

    Think my support is good? Get more personalized support via a support subscription.

    Need more help with your app? Hire Sencha Services

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

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts