From 2ca525fcc61fa36db9e35d84d17aad7fe0c11d46 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Wed, 2 Sep 2020 09:38:26 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81Android=E6=B7=B7=E5=90=88=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/build.gradle | 2 +- .../zhios/zhiying_comm/ZhiyingCommPlugin.java | 20 ++-- .../ZhiyingFlutterCommHandler.java | 7 ++ .../ZhiyingFlutterCommNativable.java | 26 +++++ .../ZhiyingFlutterCommNative.java | 109 ++++++++++++++++++ .../ZhiyingFlutterCommNativeResult.java | 45 ++++++++ example/android/app/build.gradle | 2 +- 7 files changed, 196 insertions(+), 15 deletions(-) create mode 100644 android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java create mode 100644 android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java create mode 100644 android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java create mode 100644 android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java diff --git a/android/build.gradle b/android/build.gradle index 756283b..c2643ba 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -25,7 +25,7 @@ android { compileSdkVersion 28 defaultConfig { - minSdkVersion 16 + minSdkVersion 19 testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } lintOptions { diff --git a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java index d570c74..5ca96df 100644 --- a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java +++ b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java @@ -9,11 +9,12 @@ import io.flutter.plugin.common.MethodChannel.Result; import io.flutter.plugin.common.PluginRegistry.Registrar; /** ZhiyingCommPlugin */ -public class ZhiyingCommPlugin implements FlutterPlugin, MethodCallHandler { +public class ZhiyingCommPlugin implements FlutterPlugin { @Override public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { - final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "zhiying_comm"); - channel.setMethodCallHandler(new ZhiyingCommPlugin()); +// final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "zhiying_comm"); +// channel.setMethodCallHandler(new ZhiyingCommPlugin()); + ZhiyingFlutterCommNative.getInstance().registerWith(flutterPluginBinding.getFlutterEngine().getDartExecutor()); } // This static function is optional and equivalent to onAttachedToEngine. It supports the old @@ -26,18 +27,11 @@ public class ZhiyingCommPlugin implements FlutterPlugin, MethodCallHandler { // depending on the user's project. onAttachedToEngine or registerWith must both be defined // in the same class. public static void registerWith(Registrar registrar) { - final MethodChannel channel = new MethodChannel(registrar.messenger(), "zhiying_comm"); - channel.setMethodCallHandler(new ZhiyingCommPlugin()); +// final MethodChannel channel = new MethodChannel(registrar.messenger(), "zhiying_comm"); +// channel.setMethodCallHandler(new ZhiyingCommPlugin()); + ZhiyingFlutterCommNative.getInstance().registerWith(registrar.messenger()); } - @Override - public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { - if (call.method.equals("getPlatformVersion")) { - result.success("Android " + android.os.Build.VERSION.RELEASE); - } else { - result.notImplemented(); - } - } @Override public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { diff --git a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java new file mode 100644 index 0000000..634969c --- /dev/null +++ b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java @@ -0,0 +1,7 @@ +package cn.zhios.zhiying_comm; + +import java.util.Map; + +public interface ZhiyingFlutterCommHandler { + public void handle(ZhiyingFlutterCommNativeResult result); +} diff --git a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java new file mode 100644 index 0000000..afa9193 --- /dev/null +++ b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java @@ -0,0 +1,26 @@ +/* + * @Author: your name + * @Date: 2020-06-16 15:37:18 + * @LastEditTime: 2020-06-19 17:13:52 + * @LastEditors: your name + * @Description: In User Settings Edit + * @FilePath: /hairuyi_flutter_comm/android/src/main/kotlin/com/fnuoos/hairuyi_flutter_comm/HairuyiFlutterCommNativable.java + */ +package cn.zhios.zhiying_comm; + +import java.util.Map; + +public interface ZhiyingFlutterCommNativable { + + /* 跳转公共方法 */ + public void openPage(Map params); + + /* 跳转原生页面(非公共跳转) */ + public void openNativePage(Map params); + + /* 获取设置 */ + public Map getSetting(); + + /* 普通方法(交互) */ + public void invokeMethod(String method, Map params, ZhiyingFlutterCommHandler handler); +} diff --git a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java new file mode 100644 index 0000000..0585adf --- /dev/null +++ b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java @@ -0,0 +1,109 @@ +package cn.zhios.zhiying_comm; + + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import io.flutter.plugin.common.BinaryMessenger; +import io.flutter.plugin.common.MethodCall; +import io.flutter.plugin.common.MethodChannel; +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; +import io.flutter.plugin.common.MethodChannel.Result; + +public class ZhiyingFlutterCommNative implements MethodCallHandler { + + private ZhiyingFlutterCommNativable nat; + private List natLists; + + /** + * 类级的内部类,也就是静态的成员式内部类,该内部类的实例与外部类的实例 没有绑定关系,而且只有被调用到才会装载,从而实现了延迟加载 + */ + private static class HairuyiFlutterCommNativeHolder { + /** + * 静态初始化器,由JVM来保证线程安全 + */ + private static ZhiyingFlutterCommNative instance = new ZhiyingFlutterCommNative(); + } + + private ZhiyingFlutterCommNative() { + natLists = new ArrayList<>(); + } + + public static ZhiyingFlutterCommNative getInstance() { + return HairuyiFlutterCommNativeHolder.instance; + } + + public void registerWith( BinaryMessenger message) { + MethodChannel channel = new MethodChannel(message, "zhiying_comm://method"); + channel.setMethodCallHandler(ZhiyingFlutterCommNative.getInstance()); + } + + public void regist(ZhiyingFlutterCommNativable nat) { + if (null == natLists){ natLists = new ArrayList<>();} + natLists.add(nat); + this.nat = natLists.get(natLists.size() - 1); // 获取新的 + } + + public void unregist(){ + if (null != natLists && natLists.size() > 1){ + natLists.remove(natLists.size() - 1); + nat = natLists.get(natLists.size() -1); + }else if (null != natLists && natLists.size() == 1){ + natLists.clear(); + nat = null; + } + } + + @Override + public void onMethodCall(MethodCall call, final Result result) { + + /* 公共跳转方法 */ + if (call.method.equals("openPage")) { + if (nat != null) { + nat.openPage((Map) call.arguments); + } + Map map = new HashMap(); + map.put("success", "1"); + result.success(map); + } + + /* 跳转原生页面(非公共跳转) */ + if (call.method.equals("openNativePage")) { + if (nat != null) { + nat.openNativePage((Map) call.arguments); + } + Map map = new HashMap(); + map.put("success", "1"); + result.success(map); + } + + /* 获取设置 */ + if (call.method.equals("getSetting")) { + Map map = new HashMap(); + if (nat != null) { + map = nat.getSetting(); + } + result.success(map); + } + + /* 调用原生方法 */ + if (call.method.equals("invokeMethod")) { + if (nat != null) { + Map params = ((Map) call.arguments); + nat.invokeMethod((String) params.get("method"), (Map) params.get("params"), new ZhiyingFlutterCommHandler() { + @Override + public void handle(ZhiyingFlutterCommNativeResult res) { + result.success(res.toMap()); + } + }); + } else { + result.success(ZhiyingFlutterCommNativeResult.notImp().toMap()); + } + } else { + + result.notImplemented(); + } + } +} diff --git a/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java new file mode 100644 index 0000000..93f0de4 --- /dev/null +++ b/android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java @@ -0,0 +1,45 @@ +package cn.zhios.zhiying_comm; + +import java.util.HashMap; +import java.util.Map; + +public class ZhiyingFlutterCommNativeResult { + public boolean success; + public String msg; + public Map data; + + public ZhiyingFlutterCommNativeResult(String msg, boolean success, Map data) { + this.msg = msg; + this.success = success; + this.data = data; + } + + public Map toMap() { + Map map = new HashMap<>(); + map.put("msg", this.msg); + map.put("success", this.success ? "1" : "0"); + map.put("data", this.data); + return map; + } + + public static ZhiyingFlutterCommNativeResult success(String msg) { + return new ZhiyingFlutterCommNativeResult(msg, true, null); + } + + public static ZhiyingFlutterCommNativeResult success(String msg, Map data) { + return new ZhiyingFlutterCommNativeResult(msg, true, data); + } + + public static ZhiyingFlutterCommNativeResult failed(String msg) { + return new ZhiyingFlutterCommNativeResult(msg, false, null); + } + + public static ZhiyingFlutterCommNativeResult failed(String msg, Map data) { + return new ZhiyingFlutterCommNativeResult(msg, false, data); + } + + public static ZhiyingFlutterCommNativeResult notImp() { + return new ZhiyingFlutterCommNativeResult("native调用失败", false, null); + } + +} diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index b90ebeb..980d7e5 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -34,7 +34,7 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "cn.zhios.zhiying_comm_example" - minSdkVersion 16 + minSdkVersion 19 targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName