业务场景:
安卓原生混合app(uniapp),当安卓的sdk有数据回调的时候,uniapp那边怎么实时获取到呢?不是只能通过调用函数的方式获取回调结果的,还有另一种方法,就是使用监听。
在uniapp的页面,我们可以建立监听:
javascript
let myEventManager = plus.android.importClass('com.clj.fastble.MyEventManager');
this.eventManager = myEventManager.getMyEventManager();
//新建监听器
this.myListener = plus.android.implements('com.clj.fastble.MyListener', {
onChange: function(event) {
//获取数据
//导入类
plus.android.importClass(event);
console.log(event.getData(), '=====');
}
});
//添加监听器
this.eventManager.addListener('onShowXX', this.myListener);
在安卓代码那里,创建对应的监听:
MyEvent.java
java
public class MyEvent {
//数据
private Object data;
//事件来源 字符串
private String source;
//触发对象
private Object trigger;
public MyEvent(Object data) {
this.data = data;
}
public Object getTrigger() {
return trigger;
}
@Override
public String toString() {
return "MyEvent{" +
"data=" + data +
", source='" + source + '\'' +
", trigger=" + trigger +
'}';
}
public void setTrigger(Object trigger) {
this.trigger = trigger;
}
public MyEvent(Object data, String source) {
this.data = data;
this.source = source;
}
public MyEvent() {
}
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;
}
}
MyListener.java
java
import java.util.EventListener;
public interface MyListener extends EventListener {
void onChange(MyEvent myEvent);
}
MyEventManager.java
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;
}
}
使用方式:
在需要推送的位置使用
java
MyEventManager.postMsg("一粒程序米","onShowXX");//刺激强度
这样,前端那边打印店结果就是
javascript
一粒程序米,=====
当然,也要记得移除监听,当不需要这个监听的时候,就移除
在uniapp那边
javascript
beforeDestroy() {
this.eventManager.removeListener(this.myListener);
},
学会了吗