Log.d(TAG, "onMessage: "+o);
reply.reply("ok");
}
}
2.注冊
3.dart 調用
/**
- 發送
*/
FuturesendMessage() 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 調用 原生
1.實現插件
public class FlutterPluginTest implements MethodChannel.MethodCallHandler {
private static final String TAG = "FlutterPluginTest";
/**
- 插件標識
*/
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) {
/**
- 通過 method 判斷調用方法
*/
if (methodCall.method.equals(ACTION_LOG)) {
/** - 解析參數
*/
String text = methodCall.argument(LOG_ARGUMENT);
if (TextUtils.isEmpty(text)) {
/** - 錯誤返回
*/
result.error("Data is Null",null,null);
}else {
Log.d(TAG, "onMethodCall: "+text);
/** - 成功返回
*/
result.success("is ok");
}
}else {
result.notImplemented();
}
}
}
2.注冊插件
public class MainActivity extends FlutterActivity {br/>@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
- 注冊插件
*/
FlutterPluginTest.registerWith(this.registrarFor(FlutterPluginTest.CHANNEL));
}
}
3.Flutter 端調用
import package:flutter/services.dart;
/**
- 名稱要和Java端一致
*/
const channelName = "com.mmd.flutterapp/plugin";
const methodName = "log";
const MethodChannel channel = MethodChannel(channelName);
Future
Map
String result = await channel.invokeMethod(methodName,map);
print(result);
}
EventChannel
原生發送數據到Flutter
1.實現插件
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.注冊插件
FlutterPluginEventTest.registerWith(this.registrarFor(FlutterPluginEventTest.CHANNEL));
3.Flutter 接收
import dart:async;
import package:flutter/services.dart;
最后的最后
對于程序員來說,要學習的知識內容、技術有太多太多,要想不被環境淘汰就只有不斷提升自己,從來都是我們去適應環境,而不是環境來適應我們!
當你有了學習線路,學習哪些內容,也知道以后的路怎么走了,理論看多了總要實踐的
最后,互聯網不存在所謂的寒冬,只是你沒有努力罷了!
本文已被[CODING開源項目:《Android學習筆記總結+移動架構視頻+大廠面試真題+項目實戰源碼》]( )收錄