Call Native Methods

Genero applications can call native Android methods.

Genero applications can call native Android methods. The GHC uses a JavaScriptâ„¢ API class named GHCJavascriptAPI that lets JavaScript methods call native methods. JavaScript will use the object instance created by the JavaScript API class given to the webview to call native methods.

For ease of use, developers call this class method by another function, gwc.ghc.invoke('functionName', params). It needs a function name and a JSON object as parameters.

In JavaScript:
...
function setMyGeneroApplicationName() {
       var data = {};
       data.name = 'Application name';
       data.description = 'some description';

       gwc.ghc.invoke('setGeneroApplicationName', data);
}
...
In the java class file, you will need to decode given parameters and create a JSONObject object to get parameters attributes. Another parameter (vCallbackId in this example) is the callbackId that you need to call callback JavaScript functions. The best example to use callback functions is the NativeYesNo method in the GHCJavascriptAPI class file.
public class GHCJavascriptAPI {
       ...
       public void setGeneroApplicationName(String vArgs, String vCallbackId) {
              String args = null;
              String name = null;
              String description = null;

              // Decode URI and remove quotes
              try {
                     args = URLDecoder.decode(vArgs, "UTF-8");
              } catch (UnsupportedEncodingException e) {
                     e.printStackTrace();
              }
              try {
                     JSONObject jsonObject = new JSONObject(args);
                     name = jsonObject.getString("name");
                     description = jsonObject.getString("description");
              } catch (JSONException e) {
                     e.printStackTrace();
              }
              ...
       }
       ...
}