简体   繁体   English

Phonegap Java调用到Javascript

[英]Phonegap Java call to Javascript

I'm using Phonegap to develop an app. 我正在使用Phonegap开发应用程序。 I've downloaded a camera plugin, however, I'd like to make a Javascript call from within the plugin. 我已经下载了相机插件,但是,我想从插件中进行Java调用。

In the Java file for the camera plugin I have done the following; 在相机插件的Java文件中,我执行了以下操作;

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... 但是,当触发onclick时,什么也没有发生...

I've also tried super.sendJavascript() and super.loadUrl() but it didn't work. 我也尝试了super.sendJavascript()和super.loadUrl(),但是没有用。

Thanks. 谢谢。

You have two ways to comunicate to your javascript code. 您可以通过两种方式来交流您的JavaScript代码。 The first one is injecting code to webview via .loadUrl(...) method. 第一个是通过.loadUrl(...)方法将代码注入到webview中。 The second one is via a callback in response to a javascript->native-plugin(java) call. 第二个是通过回调来响应javascript-> native-plugin(java)调用。

You can see callback response in execYourJavaMethod() and injecting in sendcommand() 您可以在execYourJavaMethod()中看到回调响应,并在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: 从javascript方面,您应该注册事件的侦听器:

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

To comunicate to your Java plugin: 要与您的Java插件通讯:

myPlugin.doSomethingInJava = function (successCallback, failureCallback) {
  cordova.exec(successCallback, failureCallback, 'sendJS', 'yourJavaMethod', []);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM