【问题标题】:PhoneGap - return custom plugin callback value from method other than execute()PhoneGap - 从 execute() 以外的方法返回自定义插件回调值
【发布时间】:2014-09-24 01:56:58
【问题描述】:

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

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

有谁知道如何在 @Override 函数中的 callbackContext 中传递一个值?这些函数都在同一个类中。我需要启动一个新的 Intent/Activity 吗?

插件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();
    }
}
}

【问题讨论】:

    标签: android cordova plugins


    【解决方案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);
                }
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多