gpt4 book ai didi

java - 如何将数据从 Java Activity 传递到 React Native?

转载 作者:太空宇宙 更新时间:2023-11-04 09:03:59 25 4
gpt4 key购买 nike

我无法将数据从 java Activity 传递到 native react 。我正在处理卡付款,付款完成后,响应存储在名为消息的变量中。我需要将此消息传递给我的 react native 代码。

//Java模块,我要传递的数据在“message”中

公共(public)类HelloWorldModule扩展了ReactContextBaseJavaModule实现ActivityEventListener{ Activity Activity ;

ReactApplicationContext react 上下文; 公共(public)HelloWorldModule(ReactApplicationContextreactContext, Activity Activity ){ super (reactContext);//React Native 需要 this.reactContext=reactContext; this.activity= Activity ; ReactContext.addActivityEventListener(this);//将此 native 模块注册为 Activity 结果监听器

}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
// ReactApplicationContext reactContext = this.getReactNativeHost().getReactInstanceManager().getCurrentReactApplicationContext();
// reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
// reactContext
// .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
// .emit('message', message);

/*
* We advise you to do a further verification of transaction's details on your server to be
* sure everything checks out before providing service or goods.
*/
if (requestCode == RaveConstants.RAVE_REQUEST_CODE && data != null) {
String message = data.getStringExtra("response");
// Log.e("RAVE",message);
if (resultCode == RavePayActivity.RESULT_SUCCESS) {
Toast.makeText(activity, "SUCCESS " + message, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RavePayActivity.RESULT_ERROR) {
Toast.makeText(activity, "ERROR " + message, Toast.LENGTH_SHORT).show();
}
else if (resultCode == RavePayActivity.RESULT_CANCELLED) {
Toast.makeText(activity, "CANCELLED " + message, Toast.LENGTH_SHORT).show();
}
}
// else {
// super.onActivityResult(activity, requestCode, resultCode, data);
// }

}

//@覆盖//public void onActivityResult(Activity Activity, int requestCode, int resultCode, Intent data) {//Toast.makeText( Activity , "hello", Toast.LENGTH_SHORT).show();//}

@Override

公共(public)无效onNewIntent( Intent Intent ){

}

@Override
//getName is required to define the name of the module represented in JavaScript
public String getName() {
return "HelloWorld";
}

@ReactMethod
public void sayHi(Callback errorCallback, Callback successCallback) {
try{
int amount = 30;//call.argument("amount");
String narration = "Payment for soup";//call.argument("nara");
String countryCode = "NG"; //call.argument("countryCode");
String currency = "NGN"; //call.argument("currency");
String amountText = "50";//call.argument("amountText");
String email = "*****@yahoo.com";//call.argument("email");
String name = "Ubanna Danny";//call.argument("name");
String paymentId = "a98sjkhdjdu";//call.argument("paymentId");



String key ="FLWPUBK-****-X";
String encryptionKey = "****";

new RavePayManager(activity).setAmount(Double.parseDouble(String.valueOf(amount)))
.setCountry(countryCode)
.setCurrency(currency)
.setEmail(email)
.setfName(name)
.setlName("")
.setNarration(narration)
.setPublicKey(key)
.setEncryptionKey(encryptionKey)
.setTxRef(paymentId)
.acceptMpesaPayments(false)
.acceptAccountPayments(true)
.acceptCardPayments(true)
.acceptGHMobileMoneyPayments(false)
.onStagingEnv(false)
.allowSaveCardFeature(true)
.initialize();
} catch (IllegalViewOperationException e) {
errorCallback.invoke(e.getMessage());
}
}

}

//React 原生代码

   // async function to call the Java native method
async sayHiFromJava() {
HelloWorld.sayHi( (err) => {console.log(err)}, (msg) => {console.log(msg)} );
}

请帮忙。

最佳答案

使用设备发射器将数据从 native 发送到 react native

在 Activity 结果中添加以下代码

ReactContext context = this.getReactNativeHost().getReactInstanceManager().getCurrentReactContext();

context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit();
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
.emit('message', message);

在React Native中添加Dives发射器监听器

import {DeviceEventEmitter} from 'react-native';

EmitterModule.addListener('message', (message) => {
console.log(message);
};

关于java - 如何将数据从 Java Activity 传递到 React Native?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60447516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com