0

I'm using Phonegap to develop an app. I've downloaded a camera plugin, however, I'd like to make a Javascript call from within the plugin.

In the Java file for the camera plugin I have done the following;

private class sendJS extends CordovaActivity {
    public void sendcommand() {
        this.sendJavascript("alert('1337')");
    }
}

@Override
public void onClick(View v) {
    sendJS test = new sendJS();
    test.sendcommand();
}

However, when the onclick is triggered nothing happens...

I've also tried super.sendJavascript() and super.loadUrl() but it didn't work.

Thanks.

3
  • You should use the exec() command inside Cordova, see cordova.apache.org/docs/en/3.5.0/… Commented Sep 16, 2014 at 17:07
  • 1
    @MBillau: Looks like that is in the Javascript side, not the Java side. The code he has above is the Java side. Commented Sep 16, 2014 at 17:10
  • @DarkFalcon, you are right. I'm trying to do exactly the opposite of that... Commented Sep 16, 2014 at 21:06

1 Answer 1

2

You have two ways to comunicate to your javascript code. The first one is injecting code to webview via .loadUrl(...) method. The second one is via a callback in response to a javascript->native-plugin(java) call.

You can see callback response in execYourJavaMethod() and injecting in sendcommand()

private class sendJS extends CordovaActivity {
  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    // Implement here your calls from javascript
    boolean result = false;

    if ("yourJavaMethod".equals(action)) {
      JSONObject options = args.optJSONObject(0);
      result = execYourJavaMethod(options, callbackContext);
    }

    return result;
  }

  public boolean execYourJavaMethod(JSONObject options, CallbackContext callbackContext) {
    // This will inject an event to your javascript code
    this.sendcommand();

    boolean iWantToCallSuccessCallbackWithData = false;
    if (iWantToCallSuccessCallbackWithData) {
      // This will call your success callback with some data
      callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Your data back to javascript"));

    } else {
      // This will call the success callback with no data
      callbackContext.success();
    }

    return true;
  }

  public void sendcommand() {
    String event = String.format("javascript:cordova.fireDocumentEvent('yourEventHere', { 'param1': '%s' });", "some string for param1");
    this.webView.loadUrl(event);
  }
}

From javascript side you should register the listener for your event:

document.addEventListener('yourEventHere', function(e) {
  alert(JSON.stringify(e));
});

To comunicate to your Java plugin:

myPlugin.doSomethingInJava = function (successCallback, failureCallback) {
  cordova.exec(successCallback, failureCallback, 'sendJS', 'yourJavaMethod', []);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.