Browse Source

1、Android混合代码

tags/0.0.1
PH2 4 years ago
parent
commit
2ca525fcc6
7 changed files with 196 additions and 15 deletions
  1. +1
    -1
      android/build.gradle
  2. +7
    -13
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java
  3. +7
    -0
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java
  4. +26
    -0
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java
  5. +109
    -0
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java
  6. +45
    -0
      android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java
  7. +1
    -1
      example/android/app/build.gradle

+ 1
- 1
android/build.gradle View File

@@ -25,7 +25,7 @@ android {
compileSdkVersion 28

defaultConfig {
minSdkVersion 16
minSdkVersion 19
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {


+ 7
- 13
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingCommPlugin.java View File

@@ -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) {


+ 7
- 0
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommHandler.java View File

@@ -0,0 +1,7 @@
package cn.zhios.zhiying_comm;

import java.util.Map;

public interface ZhiyingFlutterCommHandler {
public void handle(ZhiyingFlutterCommNativeResult result);
}

+ 26
- 0
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativable.java View File

@@ -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<String, Object> params, ZhiyingFlutterCommHandler handler);
}

+ 109
- 0
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNative.java View File

@@ -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<ZhiyingFlutterCommNativable> 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<String, Object>();
map.put("success", "1");
result.success(map);
}

/* 跳转原生页面(非公共跳转) */
if (call.method.equals("openNativePage")) {
if (nat != null) {
nat.openNativePage((Map) call.arguments);
}
Map map = new HashMap<String, Object>();
map.put("success", "1");
result.success(map);
}

/* 获取设置 */
if (call.method.equals("getSetting")) {
Map map = new HashMap<String, Object>();
if (nat != null) {
map = nat.getSetting();
}
result.success(map);
}

/* 调用原生方法 */
if (call.method.equals("invokeMethod")) {
if (nat != null) {
Map params = ((Map<String, String>) 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();
}
}
}

+ 45
- 0
android/src/main/java/cn/zhios/zhiying_comm/ZhiyingFlutterCommNativeResult.java View File

@@ -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<String, Object> data;

public ZhiyingFlutterCommNativeResult(String msg, boolean success, Map<String, Object> 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<String, Object> 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<String, Object> data) {
return new ZhiyingFlutterCommNativeResult(msg, false, data);
}

public static ZhiyingFlutterCommNativeResult notImp() {
return new ZhiyingFlutterCommNativeResult("native调用失败", false, null);
}

}

+ 1
- 1
example/android/app/build.gradle View File

@@ -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


Loading…
Cancel
Save