在uni-app开发过程中,有时候会遇到uni-app插件或者提供的api对硬件操作不太友好,需要使用原生Android开发
对应模块,为了使得双方通信方便,特意封装了一个接口,可实现Android与Uni-app互相通讯。
二、内容
做完以下第一、第二部分,即可实现Android与uni-app 互相通信,当然双方通信有不同方式,具体情况具体分析,我的采用的方案是写Android原生插件,在uni-app中集成Android原生插件方式实现。
1、原生Android部分
Android原生涉及到的几个类,MyEvent.java(传递消息的ben类),MyEventManager.java类作用(事件管理器用来处理事件与通知监听器),MyListener.java类,作用接口回调(监听器接口)。由于采用的是uni-app插件方式,故先去uni-app官网下载插件sdk进行开发。详情见最后的代码连接,以下只是例举用的类。
代码
public class MyEvent {//数据private Object data;//事件来源 字符串private String source;//触发对象private Object trigger;private int state;public MyEvent() {}public MyEvent(Object data) {this.data = data;}public Object getTrigger() {return trigger;}public void setTrigger(Object trigger) {this.trigger = trigger;}public MyEvent(Object data, String source) {this.data = data;this.source = source;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public String getSource() {return source;}public void setSource(String source) {this.source = source;}public int getState() {return state;}public void setState(int state) {this.state = state;}@Overridepublic String toString() {return "MyEvent{" +"data=" + data +", source='" + source + '\'' +", trigger=" + trigger +", state=" + state +'}';}}
MyEventManager.java
/*** 事件管理器* 用来* 处理* 事件* 与通知* 监听器*/
public class MyEventManager {private static MyEventManager myEventManager;private Map<String, Collection<MyListener>> listeners;/*** 不能外部 new* 实例化*/private MyEventManager() {this.listeners = new HashMap<String, Collection<MyListener>>();}/*** 返回监听 总数** @return*/public int getSize() {int size = 0;for (String str : listeners.keySet()) {size = size + listeners.get(str).size();}return size;}public Map<String, Collection<MyListener>> getListeners() {return listeners;}/*** 单例模式** @return*/public static MyEventManager getMyEventManager() {if (myEventManager == null) {synchronized (MyEventManager.class) {if (myEventManager == null) {myEventManager = new MyEventManager();}}}return myEventManager;}/**** 添加事件* @param listener 事件对象* @param source 来源*/public MyListener addListener(MyListener listener, String source) {if (listener != null && source != null) {Collection<MyListener> myListeners = listeners.get(source);if (myListeners == null) {myListeners = new HashSet<MyListener>();listeners.put(source, myListeners);}myListeners.add(listener);}return listener;}/**** 添加事件* @param source 来源* @param listener 事件对象*/public MyListener addListener(String source, MyListener listener) {return addListener(listener, source);}/*** 移除监听** @param listener*/public void removeListener(MyListener listener) {if (listeners == null || listener == null) {return;}//变量所有 找出相同的 删除for (String str : listeners.keySet()) {Collection collection = listeners.get(str);Iterator<MyListener> iter = collection.iterator();while (iter.hasNext()) {MyListener next = (MyListener) iter.next();if (next == listener) {collection.remove(next);return;}}}}/**** 发送数据* @param data 数据* @param source 来源* @return*/public static MyEvent postMsg(Object data, String source) {MyEventManager myEventManager = MyEventManager.getMyEventManager();MyEvent myEvent = new MyEvent(data);myEvent.setSource(source);if (myEventManager.listeners == null)return myEvent;myEventManager.notifyListeners(myEvent, myEvent.getSource());return myEvent;}/*** 通知所有的myListener* 相同的 (source) 来源才通知*/private void notifyListeners(MyEvent event, String source) {//取出 key为source 的 监听器集合Collection<MyListener> collection = listeners.get(source);// Log.i(MyEventManager.class.getName(), source + "--->" + event.getData());if (collection == null) {return;}//遍历监听器集合Iterator<MyListener> iter = collection.iterator();while (iter.hasNext()) {MyListener next = iter.next();//通知回调next.onChange(event);}//销毁事件对象event = null;}}
MyListener.java类
public interface MyListener extends EventListener {void onChange(MyEvent myEvent);
}
BluetoothAndWifi.java类继承UniModule实现插件
public class BluetoothAndWifi extends UniModule {public static int REQUEST_CODE = 1000;//请求码public static int REQUEST_BLUETOOTH_CODE = 1001;//请求码@UniJSMethod(uiThread = false)public void sendMsgToUni() {MyEventManager.postMsg("==大宝==Android发送给==Uni-app====", "formAndroid");}@UniJSMethod(uiThread = false)public void initAndroidEventListner() {Ted();}@UniJSMethod(uiThread = true)public void Ted() {Toast.makeText(mWXSDKInstance.getContext(), "==初始化监听AndroidEventListner==", Toast.LENGTH_SHORT).show();MyEventManager.getMyEventManager().addListener(new MyListener() {@Overridepublic void onChange(MyEvent myEvent) {if (myEvent.getSource().trim().equals("fromUni-app") ) {//判断过滤//从uniapp 或者原生传过来的数据Object data = myEvent.getData();Toast.makeText(mWXSDKInstance.getContext(), "==Uni传过来的数据====" + data, Toast.LENGTH_SHORT).show();}}}, "fromUni-app");}/*** 加法运算*/@UniJSMethod(uiThread = false)public void addMathNumber(int a, int b, UniJSCallback callback) {callback.invoke(a + b + 9);}/*** 原生toast提示*/@UniJSMethod(uiThread = false)public void showToast(String msg, UniJSCallback callback) {Toast.makeText(mWXSDKInstance.getContext(), msg, Toast.LENGTH_SHORT).show();callback.invoke(msg);}/*** 原生toast提示*/@UniJSMethod(uiThread = false)public void aShowToastOK(String msg, UniJSCallback callback) {Toast.makeText(mWXSDKInstance.getContext(), msg, Toast.LENGTH_SHORT).show();callback.invoke(msg);}/*** 跳转到* wifi页面*/@UniJSMethod(uiThread = false)public void gotoWifiPage() {if (mUniSDKInstance != null && mUniSDKInstance.getContext() instanceof Activity) {Intent intent = new Intent(mUniSDKInstance.getContext(), WiFiActivity.class);((Activity) mUniSDKInstance.getContext()).startActivityForResult(intent, REQUEST_CODE);}}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == REQUEST_CODE && data.hasExtra("respond")) {
// Log.e("TestModule", "原生页面返回----"+data.getStringExtra("respond"));} else {super.onActivityResult(requestCode, resultCode, data);}}
怎么使用
Android原生部分//发送消息MyEventManager.postMsg("==大宝==Android发送给==Uni-app====", "formAndroid");//发消息//初始化监听MyEventManager.getMyEventManager().addListener(new MyListener() {@Overridepublic void onChange(MyEvent myEvent) {if (myEvent.getSource().trim().equals("fromUni-app") ) {//判断过滤//从uniapp 或者原生传过来的数据Object data = myEvent.getData();Toast.makeText(mWXSDKInstance.getContext(), "==Uni传过来的数据====" + data, Toast.LENGTH_SHORT).show();}}}, "fromUni-app");
//"fromUni-app" 标记 必须与发送消息的标记一样uni-app部分的代码//获取java类
let myEventManager = plus.android.importClass("com.xiaoya.mylibrary.business.msg.MyEventManager");
let eventManager = myEventManager.getMyEventManager();let myListener = plus.android.implements("com.xiaoya.mylibrary.business.msg.MyListener", {onChange: function(event) {plus.android.importClass(event);console.log("==日志输出33333==" + event.getData());if (event.getSource() == 'formAndroid') {andoridModule.showToast(event.getData());}//导入类 //获取数据 // console.log); //获取来源 // console.log(event.getSource(); }})// //添加监听器 eventManager.addListener("formAndroid", myListener);
msg部分
uni-app%E8%B0%83%E7%94%A8%E9%83%A8%E5%88%86">2、uni-app调用部分
a、代码
<template><view class="content"><input type="number" v-model="a" /><input type="number" v-model="b" /><button type="default" @click="sendMsgToUni">原生发消息给uni</button><button type="default" @click="add">加法</button><button type="default" @click="openwifi">打开wifi</button><button type="default" @click="openbluetooth">打开蓝牙</button><button type="default" @click="sendMsgToAndroid">Uni发消息给原生</button></view>
</template><script>// 引入插件let andoridModule = uni.requireNativePlugin('BluetoothAndWifiPlugin');let myEventManager = plus.android.importClass("com.xiaoya.mylibrary.business.msg.MyEventManager");let eventManager = myEventManager.getMyEventManager();export default {data() {return {a: 1,b: 2}},onLoad() {},onShow() {// 初始化原生数据接收监听andoridModule.initAndroidEventListner();this.initUniListener(); //初始化uni数据接收监听},methods: {// Uni发消息给原生sendMsgToAndroid() {myEventManager.postMsg("==蘑菇头==发送消息==", "fromUni-app");},/* 原生发消息给uni */sendMsgToUni() {andoridModule.sendMsgToUni();},// 初始化Uni监听initUniListener() {console.log("===日志输出111===" + myEventManager);console.log("===日志输出222===" + eventManager);let myListener = plus.android.implements("com.xiaoya.mylibrary.business.msg.MyListener", {onChange: function(event) {plus.android.importClass(event);console.log("==日志输出33333==" + event.getData());if (event.getSource() == 'formAndroid') {andoridModule.showToast(event.getData());}//导入类 //获取数据 // console.log); //获取来源 // console.log(event.getSource(); }})// //添加监听器 eventManager.addListener("formAndroid", myListener);},/*打开蓝牙*/openbluetooth() {// this.jsCallNativeActivity();// andoridModule.goToBluetoothPage();},/*openwifi页面跳转*/openwifi() {// myEventManager.postMsg("app 显示了", "onShow");andoridModule.gotoWifiPage();},/*** 相加*/add() {andoridModule.addMathNumber(this.a, this.b, (e) => {andoridModule.showToast('1+B=' + e);});},}}
</script><style>.content {padding-top: 200rpx;}
</style>
uni-app工程截图