Flutter 與 android 原生交互

一、flutter發送數據給android原生。android

android 數據接收類app

import android.app.Activity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class FlutterPluginReceive implements MethodChannel.MethodCallHandler {
    public static String CHANNEL = "com.leimo.wanbanerpPlugin";
    static MethodChannel channel;
    private Activity activity;
    
    private FlutterPluginReceive(Activity activity) {
        this.activity = activity;
    }

    public static void registerWith(PluginRegistry.Registrar registrar) {
        channel = new MethodChannel(registrar.messenger(), CHANNEL);
        FlutterPluginReceive instance = new FlutterPluginReceive(registrar.activity());
        //setMethodCallHandler 在此通道上接收方法調用的回調
        channel.setMethodCallHandler(instance);
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        if (call.method.equals("install")) {
            //安裝apk
            String path = call.argument("path");
            //InstallUtils.install(activity, path);
        } else {
            result.notImplemented();
        }
    }
}
複製代碼

在activity 註冊async

import android.os.Bundle;

import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        registerCustomPlugin(this);
    }

    private static void registerCustomPlugin(PluginRegistry registrar) {
        FlutterPluginReceive.registerWith(registrar.registrarFor(FlutterPluginReceive.CHANNEL));
    }
}
複製代碼

flutter 發送數據ide

import 'dart:async';
import 'package:flutter/services.dart';

//與android交互
class ChannelAndroid {
  static String CHANNEL = "com.leimo.wanbanerpPlugin";
  static String INSTALL = "install";

  // 安裝
  static Future<Null> installApk(path) async {
    try {
      await MethodChannel(CHANNEL).invokeMethod(INSTALL, {'path': path});
    } on PlatformException catch (_) {}
  }
}
複製代碼

二、android 原生髮送數據給flutter。this

android 原生髮送消息類spa

import android.util.Log;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class FlutterPluginSend implements EventChannel.StreamHandler {

    public static String CHANNEL = "com.leimo.wanbanerp.send.Plugin";

    public static String sMsg;

    //發送消息
    public static void sendMsg(BinaryMessenger binaryMessenger, String msg) {
        sMsg = msg;
        EventChannel channel = new EventChannel(binaryMessenger, CHANNEL);
        FlutterPluginSend instance = new FlutterPluginSend();
        channel.setStreamHandler(instance);
    }

    @Override
    public void onListen(Object o, final EventChannel.EventSink eventSink) {
        //開始發送消息
        eventSink.success(sMsg);
    }

    @Override
    public void onCancel(Object o) {
        Log.i("FlutterPluginSend", "FlutterPluginSend:onCancel");
    }
複製代碼

flutter 註冊監聽接收消息code

static String CHANNEL = 'com.leimo.wanbanerp.send.Plugin';
StreamSubscription _subscription;

  @override
  void initState() {
    super.initState();
    _subscription = EventChannel(CHANNEL).receiveBroadcastStream().listen(
      (Object value) {
        ToastUtil.show("收到$value");
      },
    );
  }
  
  @override
  void dispose() {
    if (_subscription != null) {
      _subscription.cancel();
      _subscription = null;
    }
    super.dispose();
  }複製代碼
相關文章
相關標籤/搜索