目录
一、背景
在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;
}
@Override
public 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() {
@Override
public 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);
}
}
@Override
public 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() {
@Override
public 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部分
![](https://i-blog.csdnimg.cn/blog_migrate/fee979955ade638f2dcdb070bd338964.png)
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工程截图
![](https://i-blog.csdnimg.cn/blog_migrate/b681db50758b9e12639a4cbaf7218f54.png)
3、最终效果
![](https://i-blog.csdnimg.cn/blog_migrate/a70d038c31d8546d36ef4c1fbf498b9c.png)
4、代码地址
代码还包含其他功能,例如uni-app调用
Android原生代码
https://download.csdn.net/download/shi450561200/87383612
uni-app代码