2014-09-24 48 views
0

我有一个自定义的PhoneGap插件,它导入了一个jar文件。 jar文件允许我创建一些@Override函数,我需要使用插件从这些函数中返回一个值。因此我不能简单地在execute()中调用callbackContext.success(value)来返回我的值。PhoneGap - 从execute()方法返回自定义插件回调值()

我试着在我的@Override函数中调用callbackContext.success(value),但这不起作用。它只是导致应用程序崩溃。

有谁知道如何在@Override函数中传递callbackContext中的值吗?这些功能都在同一个类中。我是否需要开始新的意图/活动?

插件Java代码:

package com.cordova.plugin.scan; 

import org.apache.cordova.CallbackContext; 
import org.apache.cordova.CordovaPlugin; 
import org.apache.cordova.PluginResult; 
import org.json.JSONArray; 
import org.json.JSONException; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnCompletionListener; 
import android.os.Message; 
import android.util.Log; 
import android.content.Context; 

import com.phychips.rcp.*; 

public class Scan extends CordovaPlugin implements iRcpEvent2, OnCompletionListener { 

public CallbackContext callbackContext; 

@Override 
public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
{ 
     try { 
      RcpApi2 rcpAPI = RcpApi2.getInstance(); 
      rcpAPI.setOnRcpEventListener(this); 
      try { 
       boolean t = rcpAPI.open(); 
       setVolumeMax(); 
       if (t = true) { 
        try {     
         boolean k = rcpAPI.startReadTagsWithRssi(maxTags, 
           maxTime, repeatCycle); 
         if (k = true) {  

          return true; 
         } 
        } catch (final Exception e) { 
         e.printStackTrace(); 
        } 
       } else { 
        return false; 
       } 
      } catch (final Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     catch (final Exception e) { 
      e.printStackTrace(); 
     } 
     return false; 
} 

@Override 
public void onTagMemoryReceived(final int[] data) { 
    // TODO Auto-generated method stub 
    try { 
     cordova.getActivity().runOnUiThread(new Runnable() { 
      public void run(){ 
       String dataText = RcpLib.int2str(data); 
       callbackContext.success(dataText); 
      } 
     }); 
    } 
    catch (final Exception e) { 
      e.printStackTrace(); 
    } 
} 
} 

回答

1

的情况下,在你的类想通了这一点有人想知道...

集CallbackContext callbackContext。 在你的exec()函数中设置this.callbackContext。 在@Override函数中使用callbackContext.sendPluginResult(value)。

public class Scan extends CordovaPlugin{ 
    private CallbackContext callbackContext; 

    @Override 
    public boolean execute (String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
    { 
     this.callbackContext = callbackContext; 
    } 

    @Override 
    public void onTagMemoryReceived(final int[] data) { 
     cordova.getActivity().runOnUiThread(new Runnable() { 
      public void run(){ 
       String dataText = RcpLib.int2str(data); 

        PluginResult result = new PluginResult(PluginResult.Status.OK, dataText); 
        result.setKeepCallback(false); 
        callbackContext.sendPluginResult(result); 
      } 
     }); 
    } 
}