如何从 Java 中的嵌套内部 class 发送事件

How to send event from nested inner class in Java

我有外public class ClassifierModule和内public class ClassifierRunnable。如何从内部 class 发送 int 并在外部接收它? 这是我的代码:

//outer class
public class ClassifierModule extends ReactContextBaseJavaModule implements BufferListener {

    public ClassifierModule(ReactApplicationContext reactContext) {
        super(reactContext);
        appState = ((MainApplication)reactContext.getApplicationContext());
        }     

    @Override
    public String getName() {
      return "Classifier";
    }

    // Called to emit events to event listeners in JS
    private void sendEvent(String eventName, int result) {
        getReactApplicationContext()
           .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
           .emit(eventName, result);
    }

    //inner class        
    public class ClassifierRunnable implements Runnable {

        public ClassifierRunnable(double[][] buffer) {
            rawBuffer = buffer;
            PSD = new double[NUM_CHANNELS][nbBins];
        }

        @Override
        public void run() {
            if (noisePresent(rawBuffer)) {
                Log.w("ConcentrationTracker", "noise");

                return;
            }

            else if(isTracking) {
                getSmoothPSD(rawBuffer);

                bandMeans = bandExtractor.extract1D(PSD);

                // Concentration algorithm goes here
                int score = measureRatio(bandMeans);

                Log.w("ConcentrationTracker", "Concentration score" + score);

                sendEvent("CONCENTRATION_SCORE", score);
            }

            else if(isLogging) {
                getSmoothPSD(rawBuffer);

                bandMeans = bandExtractor.extract1D(PSD);

                int a = measureAlpha(bandMeans);
                int b = measureBeta(bandMeans);

                String csvLine = a + "," + b;

            }
    }
}

您可以在外部 class 中指定一个方法,它接受 int 值并从内部 class 调用它。这样你就可以将数据传输到外部 class.

看起来你已经有一个函数可以做到这一点...只需截取整数

private void sendEvent(String eventName, int result) {
  System.out.println("Got " + result);
  // other code 
}