使用FlutterPlugin时,如何聆听起始攻击呼叫?

发布于 2025-01-21 09:21:18 字数 3566 浏览 0 评论 0 原文

我正在使用Java开发用于Android的弹奏插件。当我调用方法通道时,我需要调用另一个Android意图。如果我使用的是本机活动,那将很简单,因为我可以调用 startActivityForresult 并在同一类中实现方法 onActivityResult

但是,当我开发一个颤音插件时,我可以实现 flutterplugin methodCallHandler activityaware 接口,但是我如何开始新的活动并聆听异步结果?

Java代码:

public class GetnetPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

  private MethodChannel channel;
  private Context context;
  private Activity activity;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    context = flutterPluginBinding.getApplicationContext();
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "com.example/method");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method){
      case "makePayment":
        makePayment();
        result.success(true);
        return;
      default:
        result.notImplemented();
        return;
    }
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    this.activity = null;
  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivity() {
    this.activity = null;
  }

  //This is the method that is called with main action. It starts a new activity by a new intent, then it should listen for a async result
  public void makePayment(){
    Bundle bundle = new Bundle();
    DecimalFormat df = new DecimalFormat("000000000000");
    bundle.putString("amount",  df.format(10));
    bundle.putString("paymentType", "credit");

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("getnet://pagamento/v3/payment"));
    intent.putExtras(bundle);

    if(intent != null) {
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      activity.startActivityForResult(intent, REQUEST_CODE);    
    }
  }

  //This is the method that I need to be called after new activity returns a result
  public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
      @Override
      public void run() {
        channel.invokeMethod("success", data);
      }
    });
  }
}

颤音一侧:

class Getnet {
  final MethodChannel _channel =
      const MethodChannel('com.example/method');

  final StreamController _streamController = StreamController.broadcast();

  Getnet() {
    _channel.setMethodCallHandler((call) async {
      final method = call.method;
      switch (method) {
        case 'success':
          _streamController.sink.add({
            "status": 1,
            "msg": call.arguments,
          });
          break;
        case 'error':
          _streamController.sink.add({
            'status': 0,
            'msg': call.arguments['errorMessage'],
          });
          break;
      }
    });
  }

  void dispose() {
    _streamController.close();
  }

  Stream get stream => _streamController.stream;

Future makePayment() async {
    await _channel.invokeMethod('makePayment');
  }
}

I'm developing a Flutter Plugin for Android using Java. When i call the MethodChannel, I need to call another Android Intent. If I was using an native activity, it would be simple since I can call startActivityForResult and implement the method onActivityResult on the same class.

But, when I develop a Flutter Plugin, I can implement FlutterPlugin, MethodCallHandler and ActivityAware interfaces but how can I start a new activity and listen for async result?

Java code:

public class GetnetPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {

  private MethodChannel channel;
  private Context context;
  private Activity activity;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    context = flutterPluginBinding.getApplicationContext();
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "com.example/method");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    switch (call.method){
      case "makePayment":
        makePayment();
        result.success(true);
        return;
      default:
        result.notImplemented();
        return;
    }
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {
    this.activity = null;
  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
    this.activity = binding.getActivity();
  }

  @Override
  public void onDetachedFromActivity() {
    this.activity = null;
  }

  //This is the method that is called with main action. It starts a new activity by a new intent, then it should listen for a async result
  public void makePayment(){
    Bundle bundle = new Bundle();
    DecimalFormat df = new DecimalFormat("000000000000");
    bundle.putString("amount",  df.format(10));
    bundle.putString("paymentType", "credit");

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("getnet://pagamento/v3/payment"));
    intent.putExtras(bundle);

    if(intent != null) {
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      activity.startActivityForResult(intent, REQUEST_CODE);    
    }
  }

  //This is the method that I need to be called after new activity returns a result
  public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    new Handler(Looper.getMainLooper()).post(new Runnable() {
      @Override
      public void run() {
        channel.invokeMethod("success", data);
      }
    });
  }
}

Flutter side:

class Getnet {
  final MethodChannel _channel =
      const MethodChannel('com.example/method');

  final StreamController _streamController = StreamController.broadcast();

  Getnet() {
    _channel.setMethodCallHandler((call) async {
      final method = call.method;
      switch (method) {
        case 'success':
          _streamController.sink.add({
            "status": 1,
            "msg": call.arguments,
          });
          break;
        case 'error':
          _streamController.sink.add({
            'status': 0,
            'msg': call.arguments['errorMessage'],
          });
          break;
      }
    });
  }

  void dispose() {
    _streamController.close();
  }

  Stream get stream => _streamController.stream;

Future makePayment() async {
    await _channel.invokeMethod('makePayment');
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尽揽少女心 2025-01-28 09:21:18

我刚刚在Enrico Ori的帖子上找到了答案:

解决方案是使插件实施 active> activity> activity> activity> active> active> code> pluginRegistry.actinregistion.activityRestive restivityResultlistlistlistener ,它要求 OnActivityResult 实现。

I just found the answer on Enrico Ori's post at Medium: https://medium.com/theotherdev-s/mastering-flutter-create-a-plugin-e81242b6065

The solution is to make Plugin implements ActivityAware and PluginRegistry.ActivityResultListener, which demands onActivityResult implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文