我刚开始响应本机,但想知道是否可以从Java调用方法?我有一个利基设备,有一个物理按钮,我想用它来触发一个方法时,按下。
我看到了下面的插件https://github.com/artemyarulin/react-native-eval,但安卓的实现似乎已经过时了。
我已经搜索过了,但是所有的例子我都可以找到引用,从调用Java方法,而不是相反。
在此之前,非常感谢您。
编辑
我尝试使用下面@ide建议的方法来实现一个解决方案。到目前为止,我的尝试如下:
在我的MainActivity中有:
private String latestBarcode = "";
...
public String getLastestBarcode() {
return this.latestBarcode;
}
public void setLatestBarcode(String barcode) {
this.latestBarcode = barcode;
}
@Override
public void onBarcodeEvent(final BarcodeReadEvent event) {
runOnUiThread(new Runnable(){
@Override
public void run() {
String barcodeData = event.getBarcodeData();
String tstp = event.getTimestamp();
setLatestBarcode(barcodeData);
Log.d("MainActivity", " TEST - barcodeData "+ barcodeData + " tstp : "+ tstp);
}
});
}
...
当用户按下物理硬件按钮时,此代码将从设备中获取一个条形码值。
在我的自定义React本机模块中
@ReactMethod
public void latestBarcode(Callback callback) {
final Activity activity = getCurrentActivity();
if(activity != null && activity instanceof MainActivity){
callback.invoke(((MainActivity) activity).getLastestBarcode());
}
else {
callback.invoke("");
}
}
此方法接受该值并将其公开以响应本机。这是可以读取值的,但是当用户按下按钮时,我需要触发一个方法,所以这里是我的ReactiveNative模块,它现在是完整的:
import android.app.Activity;
import android.util.Log;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;
import javax.annotation.Nullable;
// Import MainActivity
import com.myapp.MainActivity;
public class HoneywellCT50Module extends ReactContextBaseJavaModule {
private ReactContext reactContext;
public HoneywellCT50Module(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
}
@ReactMethod
public void latestBarcode(Callback callback) {
final Activity activity = getCurrentActivity();
if(activity != null && activity instanceof MainActivity){
WritableMap params = Arguments.createMap(); // <--- Added WritableMap
sendEvent("BarcodeRecieved", params); // <--- Added sendEvent
callback.invoke(((MainActivity) activity).getLastestBarcode());
}
else {
callback.invoke("");
}
}
// Added sendEvent
private void sendEvent(
String eventName,
@Nullable WritableMap params
) {
this.reactContext
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit(eventName, params);
}
@Override
public String getName() {
return "HoneywellCT50";
}
}
在“土著的反应”中:
// I import DeviceEventEmitter
import { DeviceEventEmitter } from 'react-native'
componentDidMount () {
// Listen for Honeywell Scans
DeviceEventEmitter.addListener('BarcodeRecieved', this.getBarcodeValue)
}
// I want to trigger this method
getBarcodeValue () {
console.log('Event Received')
// This gets the barcode value, works ok
HoneywellCT50.latestBarcode((value) => {
this.setState({ barcode: value })
})
}
使用上面的代码,我可以在没有Java/build错误的情况下构建项目,也看不到JS错误,但我不能触发事件。有人能帮我指出正确的方向吗?
发布于 2017-01-10 15:22:03
您想要做的是从Java发出一个发送到JavaScript的事件。您可以编写一个自定义本机模块,从JavaScript订阅它的事件,然后在您的本机代码中发出事件。参见本指南:http://facebook.github.io/react-native/docs/native-modules-android.html#sending-events-to-javascript
发布于 2017-01-12 01:39:59
好的,我有一些代码在工作。我不确定这是否是最好的解决方案,但现在我的理解有限,欢迎对以下方面的任何反馈:
MainActivity.java
@Override
public void onBarcodeEvent(final BarcodeReadEvent event) {
runOnUiThread(new Runnable(){
@Override
public void run() {
String barcodeData = event.getBarcodeData();
String tstp = event.getTimestamp();
setLatestBarcode(barcodeData);
Log.d("MainActivity", " TEST - barcodeData "+ barcodeData + " tstp : "+ tstp);
}
});
WritableMap params = Arguments.createMap(); // <--- added
getReactInstanceManager().getCurrentReactContext() // <--- added
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) // <--- added
.emit("BarcodeReceived", params); // <--- added
}
通过在JS中使用以下内容,我可以触发一个React方法:
componentWillUnmount () {
this._barcodeListener.remove()
}
componentWillMount () {
// Listen for Honeywell Scans
this.getBarcodeValue = DeviceEventEmitter.addListener('BarcodeReceived', this.getBarcodeValue)
}
...
getBarcodeValue () {
console.log('Event Received')
HoneywellCT50.latestBarcode((value) => {
this.setState({ barcode: value }, () => {
console.log(this.state.barcode)
})
})
}
如果有经验的人读过这篇文章,我很想知道为什么我在上面编辑的代码不起作用。是因为我尝试在@ReactMethod
中发出事件吗?
https://stackoverflow.com/questions/41579283
复制相似问题