项目地址(github)
1、Flutter调用原生Android的api
(1)自定义插件FlutterToAndroidPlugins,实现MethodChannel.MethodCallHandler
public class FlutterToAndroidPlugins implements MethodChannel.MethodCallHandler{
private FlutterActivity activity;
private FlutterToAndroidPlugins(FlutterActivity activity) {
this.activity = activity;
}
public static void registerWith(FlutterActivity activity) {
FlutterToAndroidPlugins instance = new FlutterToAndroidPlugins(activity);
//flutter调用原生
MethodChannel channel = new MethodChannel(activity.registrarFor(DealMethodCall.channels_flutter_to_native)
.messenger(), DealMethodCall.channels_flutter_to_native);
//setMethodCallHandler在此通道上接收方法调用的回调
channel.setMethodCallHandler(instance);
}
@Override
public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
DealMethodCall.onMethodCall(activity, methodCall, result);
}
(2)DealMethodCall类处理业务
class DealMethodCall {
/**
* 通道名称,必须与flutter注册的一致
*/
static final String channels_flutter_to_native = "com.bhm.flutter.flutternb.plugins/flutter_to_native";
/**
* 方法名称,必须与flutter注册的一致
*/
private static final HashMap methodNames = new HashMap(){
{
put("register", "register");
}
};
/** flutter调用原生方法的回调
* @param activity activity
* @param methodCall methodCall
* @param result result
*/
static void onMethodCall(FlutterActivity activity, MethodCall methodCall, final MethodChannel.Result result){
if(methodNames.get("register").equals(methodCall.method)){//注册账号
//此处处理业务(线程)
result.success(object);//处理后回调给Flutter
}
}
(3)Android中的MainActivity添加注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
FlutterToAndroidPlugins.registerWith(this);
}
(4)Flutter项目中添加类InteractNative
class InteractNative {
/* 通道名称,必须与原生注册的一致*/
static const flutter_to_native = const MethodChannel(
'com.bhm.flutter.flutternb.plugins/flutter_to_native');
/*
* 方法名称,必须与flutter注册的一致
*/
static final Map methodNames = const {
'register': 'register',
};
/*
* 调用原生的方法(带参)
*/
static Future goNativeWithValue(String methodName,
[Map map]) async {
if (null == map) {
dynamic future = await flutter_to_native.invokeMethod(methodName);
return future;
} else {
dynamic future = await flutter_to_native.invokeMethod(methodName, map);
return future;
}
}
}
(5)Flutter项目中需要调用原生Android的地方,使用
Map map = {"username": username, "password": password};
InteractNative.goNativeWithValue(InteractNative.methodNames['register'], map)
.then((success) {
if (success == true) {
DialogUtil.buildToast('注册成功');
Navigator.pop(context);
} else if (success is String) {
DialogUtil.buildToast(success);
} else {
DialogUtil.buildToast('注册失败');
}
});
2、原生Android调用Flutter的api
(1)自定义插件AndroidToFlutterPlugins,实现EventChannel.StreamHandler
public class AndroidToFlutterPlugins implements EventChannel.StreamHandler{
private FlutterActivity activity;
private AndroidToFlutterPlugins(FlutterActivity activity) {
this.activity = activity;
}
public static void registerWith(FlutterActivity activity) {
AndroidToFlutterPlugins instance = new AndroidToFlutterPlugins(activity);
//原生调用flutter
EventChannel eventChannel = new EventChannel(activity.registrarFor(DealMethodCall.channels_native_to_flutter)
.messenger(), DealMethodCall.channels_native_to_flutter);
eventChannel.setStreamHandler(instance);
}
@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
DealMethodCall.onListen(activity, o, eventSink);
}
@Override
public void onCancel(Object o) {
DealMethodCall.onCancel(activity, o);
}
}
(2)DealMethodCall类处理业务
class DealMethodCall {
/**
* 通道名称,必须与flutter注册的一致
*/
static final String channels_native_to_flutter = "com.bhm.flutter.flutternb.plugins/native_to_flutter";
/**原生调用flutter方法的回调
* @param activity activity
* @param o o
* @param eventSink eventSink
*/
static void onListen(FlutterActivity activity, Object o, EventChannel.EventSink eventSink){
//在此调用
eventSink.success("onConnected");
}
/**原生调用flutter方法的回调
* @param activity activity
* @param o o
*/
static void onCancel(FlutterActivity activity, Object o) {
}
(3)Android中的MainActivity添加注册
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
AndroidToFlutterPlugins.registerWith(this);
}
(4)Flutter项目中添加类InteractNative
class InteractNative {
/* 通道名称,必须与原生注册的一致*/
static const native_to_flutter = const EventChannel(
'com.bhm.flutter.flutternb.plugins/native_to_flutter');
/*
* 原生回调的方法(带参)
*/
static Stream dealNativeWithValue(){
Stream stream = native_to_flutter.receiveBroadcastStream();
return stream;
}
}
(5)Flutter项目中使用
class _MyHomePageState extends State {
StreamSubscription _subscription = null;
@override
void initState() {
// TODO: implement initState
super.initState();
if (null == _subscription) {
_subscription = InteractNative.dealNativeWithValue()
.listen(_onEvent, onError: _onError);
}
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
if (_subscription != null) {
_subscription.cancel();
}
}
void _onEvent(Object event) {
if ('onConnected' == event) {
// DialogUtil.buildToast('已连接');
}
}
void _onError(Object error) {
DialogUtil.buildToast(error.toString());
}
}