Log.d(TAG, "onMessage: "+o);
reply.reply("ok");
}
}

2.注冊(cè)

3.dart 調(diào)用

/**

  • 發(fā)送
    */
    Future sendMessage() async{
    String reply = await messageChannel.send("Flutter send");
    print(reply);
    return reply;
    }

/**

  • 接收
    */
    void receiveMessage(){
    messageChannel.setMessageHandler((message) async{
    print(message);
    return "is ok";
    });
    }

MethodChannel

flutter 調(diào)用 原生

1.實(shí)現(xiàn)插件

public class FlutterPluginTest implements MethodChannel.MethodCallHandler {

private static final String TAG = "FlutterPluginTest";

/**

  • 插件標(biāo)識(shí)
    */
    public static String CHANNEL = "com.mmd.flutterapp/plugin";

private static String ACTION_LOG = "log";

private static String LOG_ARGUMENT = "data";

static MethodChannel channel;

public static void registerWith(PluginRegistry.Registrar registrar) {
channel = new MethodChannel(registrar.messenger(), CHANNEL);
FlutterPluginTest instance = new FlutterPluginTest();
channel.setMethodCallHandler(instance);
}

@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {

/**

  • 通過(guò) method 判斷調(diào)用方法
    */
    if (methodCall.method.equals(ACTION_LOG)) {
    /**
  • 解析參數(shù)
    */
    String text = methodCall.argument(LOG_ARGUMENT);
    if (TextUtils.isEmpty(text)) {
    /**
  • 錯(cuò)誤返回
    */
    result.error("Data is Null",null,null);
    }else {
    Log.d(TAG, "onMethodCall: "+text);
    /**
  • 成功返回
    */
    result.success("is ok");
    }
    }else {
    result.notImplemented();
    }
    }
    }

2.注冊(cè)插件

public class MainActivity extends FlutterActivity {br/>@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**

3.Flutter 端調(diào)用

import package:flutter/services.dart;

/**

const methodName = "log";

const MethodChannel channel = MethodChannel(channelName);

Future _testLog() async{

Map map = {"data":"Flutter Hello !"};

String result = await channel.invokeMethod(methodName,map);

print(result);
}

EventChannel

原生發(fā)送數(shù)據(jù)到Flutter

1.實(shí)現(xiàn)插件

public class FlutterPluginEventTest implements EventChannel.StreamHandler {

private static final String TAG = "FlutterPluginEventTest";
public static String CHANNEL = "com.mmd.flutterapp/plugin";

static EventChannel channel;

public static void registerWith(PluginRegistry.Registrar registrar) {
channel = new EventChannel(registrar.messenger(), CHANNEL);
FlutterPluginEventTest flutterPluginEventTest = new FlutterPluginEventTest();
channel.setStreamHandler(flutterPluginEventTest);
}

@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
new Thread(new Runnable() {
br/>@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
eventSink.success(System.currentTimeMillis());
} catch (InterruptedException e) {
eventSink.error("error","error",e.getMessage());
}
}
}
}).start();

}

@Override
public void onCancel(Object o) {
Log.i(TAG, "onCancel: "+o);
}
}

2.注冊(cè)插件

FlutterPluginEventTest.registerWith(this.registrarFor(FlutterPluginEventTest.CHANNEL));

3.Flutter 接收

import dart:async;

import package:flutter/services.dart;

最后的最后

對(duì)于程序員來(lái)說(shuō),要學(xué)習(xí)的知識(shí)內(nèi)容、技術(shù)有太多太多,要想不被環(huán)境淘汰就只有不斷提升自己,從來(lái)都是我們?nèi)ミm應(yīng)環(huán)境,而不是環(huán)境來(lái)適應(yīng)我們!

當(dāng)你有了學(xué)習(xí)線路,學(xué)習(xí)哪些內(nèi)容,也知道以后的路怎么走了,理論看多了總要實(shí)踐的

最后,互聯(lián)網(wǎng)不存在所謂的寒冬,只是你沒(méi)有努力罷了!

本文已被[CODING開源項(xiàng)目:《Android學(xué)習(xí)筆記總結(jié)+移動(dòng)架構(gòu)視頻+大廠面試真題+項(xiàng)目實(shí)戰(zhàn)源碼》]( )收錄