2013-05-06 89 views
0

我想创建一个salesforce插件,我想从我的java代码(这基本上是一个后台进程)推回数据到JS 但它总是给我错误线this.sendJavascript(功能名)SendJavascript函数没有定义错误科尔多瓦插件salesforce

以下是插件代码 -

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); 

    } 
} 

我已标记误差线是在末端。请帮我提前解决这个

感谢, 考希克

回答

1

sendJavascript()在两个DroidGap.java和CordovaWebView.java的功能,但在this当前文件的值是您的回音的实例。 this.sendJavascript()行失败,因为期望被调用的函数是Echo的成员或它继承的其中一个类的公共/受保护成员,在本例中为CordovaPlugin。

有在CordovaPlugin.java公共变量,命名为webView这是为您的项目CordovaWebView。如果您将违规行从this.sendJavascript(js)更改为webView.sendJavascript(js),它应该修复您的错误。

+0

其实我只是解决了这个问题..!感谢您的帮助.. !!我有另外一个怀疑我打电话给一个后台服务类的代码称为CallBackgroundService.java,但我无法将值从我的服务在Android传递给JS ...任何指针PLZ ...? – RayKaushik 2013-05-06 19:16:54

+0

很高兴听到你已经弄清楚了事情。至于你的其他问题,没有看到一些代码,我不太确定,但听起来这可能是值得发布另一个问题的东西! – MattDavis 2013-05-06 19:27:04

相关问题