问题
I am trying to create a salesforce plugin in which i want to push data back to JS from my java code (which is basically a background process) But its always giving me error for line this.sendJavascript(function name)
following is plugin code -
package com.salesforce.androidsdk.phonegap;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* This class echoes a string called from JavaScript.
*/
public class Echo extends CordovaPlugin {
private static final String TAG = "CordovaPlugin";
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
Log.i(TAG,"inside execute method--->>>" + action + args + callbackContext);
if (action.trim().equalsIgnoreCase("echo")) {
Log.i(TAG,"args.getString(0)--->>>" + args.getString(0));
String message = args.getString(0);
// Initialise the service variables and start it it up
Context thiscontext = this.cordova.getActivity().getApplicationContext();
Intent callBackgroundService = new Intent(thiscontext, CallBackgroundService.class);
callBackgroundService.putExtra("loadinterval", 800); // Set LED flash interval
thiscontext.startService(callBackgroundService);
this.echo(message, callbackContext);
sendValue("Kaushik", "Ray");
return true;
}
return false;
}
private void echo(String message, CallbackContext callbackContext) {
if (message != null && message.length() > 0) {
callbackContext.success(message);
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
public void sendValue(String value1, String value2) {
JSONObject data = new JSONObject();
try {
data.put("value1", "Kaushik");
data.put("value2", "Ray");
} catch (JSONException e) {
Log.e("CommTest", e.getMessage());
}
String js = String.format(
"window.plugins.commtest.updateValues('%s');",
data.toString());
error line ------->>> this.sendJavascript(js);
}
}
I have marked the error line which is at the end. Please help me resolve this
Thanks in Advance, Kaushik
回答1:
sendJavascript()
is a function in both DroidGap.java and CordovaWebView.java, but the value of this
in your current file is your instance of Echo. The line this.sendJavascript()
fails because is expecting the function called to be a member of Echo or a public/protected member of one of the classes it inherited from, in this case CordovaPlugin.
There is a public variable in CordovaPlugin.java, named webView
which is the CordovaWebView for your project. If you change your offending line from this.sendJavascript(js)
to webView.sendJavascript(js)
, it should fix your error.
来源:https://stackoverflow.com/questions/16404492/sendjavascript-function-not-defined-error-in-cordova-plugin-salesforce