From fa7bb03e1ba124a01c6900f28ee2a647d62a9531 Mon Sep 17 00:00:00 2001 From: zmt Date: Fri, 20 May 2022 10:12:06 +0800 Subject: [PATCH] add interface --- .../main/java/io/dcloud/zmt_module/AsyncTask.java | 4 +-- .../io/dcloud/zmt_module/MyCallbackInterface.java | 12 +++++++++ .../main/java/io/dcloud/zmt_module/zmtClass.java | 30 +++++++++++++++------- 3 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java b/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java index 84ed786..e17ed84 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java @@ -9,7 +9,7 @@ public class AsyncTask { String TAG = "zmt_module_AsyncTask "; public interface MyTcpCallback{ - void callback(Object object); + void tcpCallback(Object object); } // TCP task @@ -37,7 +37,7 @@ public class AsyncTask { // data.put("message", server.); // data.put("message", tcp_receive_message); data.put("message", tcp_receive_message); - myCallback.callback(data);//回调到zmtClass给uni端 + myCallback.tcpCallback(data);//回调到zmtClass给uni端 try { Thread.sleep(1000); //定义休眠时间 diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java b/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java new file mode 100644 index 0000000..5ecbcc8 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java @@ -0,0 +1,12 @@ +package io.dcloud.zmt_module; + +public class MyCallbackInterface { + + public interface MyTcpCallback{ + void callback(Object object); + } + + public interface UniCallback{ + void callback(Object object); + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java index 3bf9090..34355e9 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java @@ -1,6 +1,7 @@ package io.dcloud.zmt_module; import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.bridge.JSCallback; import com.tencent.iot.hub.device.android.core.gateway.TXGatewayConnection; import io.dcloud.feature.uniapp.AbsSDKInstance; @@ -14,21 +15,23 @@ import android.util.Log; import java.util.HashMap; import java.util.Map; -public class zmtClass extends UniModule { +public class zmtClass extends UniModule implements AsyncTask.MyTcpCallback{ String TAG = "zmt---"; final AbsSDKInstance uniSDK = this.mUniSDKInstance;//定义uniSDK + public UniJSCallback uniTcpStartCallback; //run ui thread @UniJSMethod(uiThread = true) public void tcpAsyncFunc(JSONObject options, final UniJSCallback callback) { Log.e(TAG, "tcpAsyncFunc--zmt:"+options); - + uniTcpStartCallback = callback; if(callback != null) { JSONObject data = new JSONObject(); data.put("msg", "已接收参数,处理中..."+options.toJSONString()); callback.invokeAndKeepAlive(data); + } Integer port = 16666;//默认端口,当uni传递port参数时,采用传递port初始化tcp @@ -40,13 +43,10 @@ public class zmtClass extends UniModule { new AsyncTask().task(new AsyncTask.MyTcpCallback() { @Override - public void callback(Object object) { -// JSONObject data = new JSONObject(); -// data.put("code", object); -// callback.invoke(data); - Map params=new HashMap<>(); - params.put("key","你好啊大家"); - uniSDK.fireGlobalEventCallback("myEvent", params);//发送全局参数直接回调 + public void tcpCallback(Object object) { + // Map params=new HashMap<>(); +// params.put("fireGlobalEventCallback","全局事件测试"); +// uniSDK.fireGlobalEventCallback("myEvent", params);//发送全局参数直接回调 callback.invokeAndKeepAlive(object);//使用jsCallback回调 } @@ -86,6 +86,18 @@ public class zmtClass extends UniModule { // }).start(); } + public void callbackOnce(JSONObject data, JSCallback callback){ +// JSONObject data = new JSONObject(); +// data.put("code", object); + callback.invoke(data); + } + + @Override + public void tcpCallback(Object object) { + System.out.println("6666666"+object.toString()); + uniTcpStartCallback.invokeAndKeepAlive(object); + } + //定义MyTcpCallback public interface MyTcpCallback{ void callback(Object object);