1

基本上我正在使用平台通道在本机 android(kotlin) 中调用支付 sdk。SDK 初始化成功。付款交易后,我将在 kotlin 文件中收到付款状态,该文件实际上扩展了 sdk 类文件(不是活动)。从这个回调方法中,我需要将状态传递给颤振飞镖代码。一切都成功运行,没有任何错误,但在传递支付状态时不会调用颤振方法。

颤振飞镖代码。

@override
void initState() {
  // TODO: implement initState
  super.initState();

  platform.setMethodCallHandler(_handlePaymentResponse); // To call from native android after payment response.

}

// Invoked from Native Android after payment response.
Future<dynamic> _handlePaymentResponse(MethodCall call) async {
  print('_handlePaymentResponse method called');
  switch(call.method) {
    case "message":
      debugPrint('From Native====' + call.arguments);
      return new Future.value("");
  }
}


// On button click this method will be invoked.
Future<void> _initializeSDK() async {

  print('Token $token');

  try {
    await platform.invokeMethod('callPaymentSDK');
  } on PlatformException catch (e) {
    print("Failed to initialize SDK : '${e.message}'.");
  }

}
Native Android : Paymentstatus.kt 

class PaymentResponse() : LibraryPaymentStatusProtocol, Parcelable {

    //val channel = MethodChannel(flutterView, MainActivity.CHANNEL)

    var backgroundFlutterView: FlutterNativeView? = null

    override fun paymentStatus(p0: String?, p1: Activity?) {

        if (p1 != null) {
            Handler(Looper.getMainLooper()).post {
                Log.v("PaymentResponse", "Main thread called")
                backgroundFlutterView = FlutterNativeView(p1, true)
                val channel = MethodChannel(backgroundFlutterView, MainActivity.CHANNEL)
                channel.invokeMethod("message", p0); // Invoking Flutter method 
            }

        }


    }
}
4

1 回答 1

1

我遇到了同样的问题,最后对我有用的解决方案(不确定这是否是理想的方法)是像这样存储对FlutterEngine内部的引用MainActivity

class MainActivity: FlutterActivity() {
    companion object {
        var flutterEngineInstance: FlutterEngine? = null
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        flutterEngineInstance = flutterEngine
    }
}

在我的代码中,我会调用这样的方法:

MethodChannel(
    MainActivity.flutterEngineInstance.dartExecutor.binaryMessenger, 
    "com.example"
).invokeMethod("method", mapOf())
于 2020-08-03T13:44:59.687 回答