美文网首页
Flutter项目中与android原生交互

Flutter项目中与android原生交互

作者: 桔子橙子柚子_F | 来源:发表于2019-10-16 15:16 被阅读0次
    1、右击项目名称,new->module新建FlutterPlugin,添加android和flutter交互的桥梁 image.png

    2、在pubspec.yaml中添加
    hello_plugin:
    path:填写路径


    准备工作到这里结束,下面看下代码。

    插件端
    android端会生成HelloPlugin.java文件,示例代码:

    /** HelloPlugin */
    public class HelloPlugin implements MethodCallHandler {
      /** Plugin registration. */
      public static void registerWith(Registrar registrar) {
        final MethodChannel channel = new MethodChannel(registrar.messenger(), "hello_plugin");
        channel.setMethodCallHandler(new HelloPlugin());
      }
    
      @Override
      public void onMethodCall(MethodCall call, Result result) {
        if (call.method.equals("getPlatformVersion")) {
          result.success("Android " + android.os.Build.VERSION.RELEASE);
        } else {
          result.notImplemented();
        }
      }
    }
    

    这里我们可以注释掉registerWith方法里的代码,放到MainActivity中注册

    new MethodChannel(getFlutterView(), "hello_plugin")
                .setMethodCallHandler((methodCall, result) -> {
                  switch (methodCall.method){
                    case "getPlatformVersion": {
                        result.success("Android " + android.os.Build.VERSION.RELEASE);
                        break;
                    }
                    default:result.notImplemented();
                  }
                });
    

    flutter端生成的hello_plugin.dart文件中 同样也会完成注册:

    class FlutterPlugin {
      static const MethodChannel _channel =
          const MethodChannel('hello_plugin');
    
      static Future<String> get platformVersion async {
        final String version = await _channel.invokeMethod('getPlatformVersion');
        return version;
      }
     
    }
    

    接下来就是传值了(flutter->android)
    hello_plugin中实现方法

    static Future<void> dosetAAA(String data) async {
           await _channel.invokeMethod("dosetAAA", {"data":data});
      }
    

    main.dart中调用该方法

    await HelloPlugin.dosetAAA("hahahaha~");
    

    MainActivity中的onMethodCall方法中判断call返回参数,处理相应逻辑

    case "dosetAAA":
     String data = call.argument("data");  //拿到参数data
    

    android->flutter参考示例platformVersion
    方法回调:
    hello_plugin.dart中注册EventChannel

    static const EventChannel _eventChannel = EventChannel("startVideoEvent");
    

    添加监听

    static eventVideoStart(EventHandler onEvent, EventHandler onError) {
        _eventChannel.receiveBroadcastStream().listen(onEvent, onError: onError);
    
      }
    

    main.dart中调用

    HelloPlugin.eventVideoStart(videoStart,errorEvent);
    

    MainActivity中实现StreamHandler,然后发射事件

    new EventChannel(getFlutterView(), "startVideoEvent")
                .setStreamHandler(new EventChannel.StreamHandler() {
                  @Override
                  public void onListen(Object o, EventChannel.EventSink eventSink) {
                    eventSink.success("true");
                  }
    
                  @Override
                  public void onCancel(Object o) {
    
                  }
                });
    

    最后记录一下android小白一点点的学习结果:
    activity和service通过Intent去传递数据,

    Intent startIntent = new Intent(this,MainActivity.class);
            startIntent.putExtras(intent);
            startActivity(startIntent);
    
    Intent intent = new Intent(MainActivity.this,RecMsgService.class);
    intent.putExtra("method", "videoSuccess");
    startService(intent);
    

    Service启动后会调用onStartCommand方法

    相关文章

      网友评论

          本文标题:Flutter项目中与android原生交互

          本文链接:https://www.haomeiwen.com/subject/cftumctx.html