提问人:Mariam Younes 提问时间:4/12/2023 更新时间:4/12/2023 访问量:309
监听 java 原生代码到 dart flutter 的回调方法
Listen to callback method from java native code to dart flutter
问:
我会与surepay集成,以在我的应用程序中添加付款 SurePay 使用 java 原生代码,就我而言,我想在我的 flutter 应用程序中使用此原生代码 一切都很好,钱取款成功,支付过程正确完成,但是有一个回调函数,我必须从中获取响应以确保支付过程成功或失败,并相应地告诉用户该怎么做。 就我而言,我无法监听本机代码中发生的变化 这是我的 java 原生代码
注意:我想在调用(sendAmount - getLastTransactionFromTerminal)方法后监听onUsbGetTransaction回调方法
public class MainActivity extends FlutterActivity implements Usb.OnReceivingData, Usb.OnConnectStatusChanges{
private static final String CHANNEL = "tkafol/channel";
private static final String MY_EVENT_CHANNEL_NAME = "tkafol/event";
private final Usb ConnParam = new Usb();
Usb.TransactionInformation model =new Usb.TransactionInformation();
private EventChannel.EventSink usbTransactionEventSink;
String transactionInfo ;
@Override
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
// Setup a method channel to receive messages from Flutter
ConnParam.setTimeout(1000 * 60 * 7);
new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), MY_EVENT_CHANNEL_NAME)
.setStreamHandler(new EventChannel.StreamHandler() {
@Override
public void onListen(Object arguments, EventChannel.EventSink eventSink) {
Log.d("Stream listen", "top listen");
Toast.makeText(getBaseContext(),"stream in native code listen",Toast.LENGTH_SHORT).show();
usbTransactionEventSink = eventSink;
}
@Override
public void onCancel(Object arguments) {
Log.d("Stream cancel", "top cancel");
Toast.makeText(getBaseContext(),"stream in native code cancel",Toast.LENGTH_SHORT).show();
usbTransactionEventSink = null;
}
});
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
(call, result) -> {
switch (call.method) {
case "sendAmount" :
String amount = call.argument("amount");
ConnParam.sendAmount(MainActivity.this, amount);
//ConnParam.sendAmount(MainActivity.this, "10.25");
result.success(null);
break;
case "sendReconciliation" :
ConnParam.sendReconciliation(MainActivity.this);
result.success(null);
break;
case "getLastTransactionFromTerminal" :
ConnParam.getLastTransactionFromTerminal(MainActivity.this);
result.success( ConnParam.getLastTransactionFromTerminal(MainActivity.this));
break;
result.notImplemented();
break;
}
});
}
// A method that calls the native code
// private boolean callNativeMethod(String message) {
// return true;
// }
@Override
public void onUsbGetTransaction(Usb.TransactionInformation model) {
Log.d("STREAM","now is null");
Toast.makeText(getBaseContext(),"stream null now",Toast.LENGTH_SHORT).show();
if (usbTransactionEventSink != null) {
// Send the event data to Flutter
Toast.makeText(getBaseContext(),"stream in native code",Toast.LENGTH_SHORT).show();
String transactionInfo = model.toString();
Map<String, String> arguments = new HashMap<>();
arguments.put("transactionInfo", transactionInfo);
new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(), CHANNEL)
.invokeMethod("onGetTransaction", arguments);
usbTransactionEventSink.success(model.toString());
}
String transactionInfo = model.toString();
Map<String, String> arguments = new HashMap<>();
arguments.put("transactionInfo", transactionInfo);
new MethodChannel(Objects.requireNonNull(getFlutterEngine()).getDartExecutor().getBinaryMessenger(), CHANNEL)
.invokeMethod("onGetTransaction", arguments);
// usbTransactionEventSink.success(model.toString());
}
}
还有我用于监听回调方法的 Flutter 代码
final EventChannel _eventChannel =
const EventChannel('tkafol/event');
StreamSubscription? _usbTransactionSubscription;
listenToEvent() {
showError("we stream before");
_usbTransactionSubscription =
_eventChannel.receiveBroadcastStream().listen((transactionInfo) {
showError("we stream after");
setState(() {
responseDataUsb = "callback listener : " + transactionInfo.toString();
});
});
}
我正在使用这个函数来监听本机代码中的事件
final EventChannel _eventChannel =
const EventChannel('tkafol/event');
StreamSubscription? _usbTransactionSubscription;
listenToEvent() {
showError("we stream before");
_usbTransactionSubscription =
_eventChannel.receiveBroadcastStream().listen((transactionInfo) {
showError("we stream after");
setState(() {
responseDataUsb = "callback listener : " + transactionInfo.toString();
});
});
}
并在 initState 方法中调用它,但总体上没有得到任何结果
答: 暂无答案
评论