我有外部public class ClassifierModule
和内部public class ClassifierRunnable
。如何从内部类发送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;
}
}
}
看起来你已经有了这样做的功能......只是截取整数
private void sendEvent(String eventName, int result) {
System.out.println("Got " + result);
// other code
}
您可以在外部类中指定一个接受int值的方法,并从内部类中调用它。这样您就可以将数据传输到外部类。