首先我们要写三个java (类|接口)
1 .事件对象 用来传递数据的
package test;
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;
}
}
2.监听器接口
package test;
import java.util.EventListener;
public interface MyListener extends EventListener {
void onChange(MyEvent myEvent);
}
3.事件管理器 用来 处理 事件 与通知 监听器
package test;
import android.util.Log;
import java.util.*;
/***
* 自定义事件管理类
*/
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;
}
}
uniapp 添加监听器 监听 原生或者 uniapp 其他页面 传过来的数据
this.myEventManager = plus.android.importClass("test.MyEventManager");
this.eventManager = this.myEventManager.getMyEventManager();
//新建监听器
this.myListener = plus.android.implements("test.MyListener", {
onChange:function(event){
//导入类
plus.android.importClass(event);
//获取数据
console.log(event.getData());
//获取来源
console.log(event.getSource();
}
})
//添加监听器
this.eventManager.addListener("onShowXX",this.myListener);
uniapp 发送数据给 原生 获取其他页面 看清 onShowXX字符串 我随意写的 只要和添加监听器的字符串 保持一致就能通知到 类似 weex 的 数据传递
this.myEventManager = plus.android.importClass("test.MyEventManager");
this.eventManager = this.myEventManager.getMyEventManager();
this.myEventManager.postMsg("app 显示了","onShowXX");
原生 接收 从uniapp 或者原生传过来的数据
MyEventManager.getMyEventManager().addListener(new MyListener() {
@Override
public void onChange(MyEvent myEvent) {
//从uniapp 或者原生传过来的数据
Object data = myEvent.getData();
}
},"onShowXX");
原生发数据 到 原生或者 uniapp
MyEventManager.postMsg("我是从原生发来的数据","onShowXX");
具体例子 app显示就发数据给原生或者 uniapp
<script>
export default {
onLaunch: function() {
console.log('App Launch')
this.myEventManager = plus.android.importClass("test.MyEventManager");
this.eventManager = this.myEventManager.getMyEventManager();
//新建监听器
this.myListener = plus.android.implements("test.MyListener", {
onChange:function(event){
//导入类
plus.android.importClass(event);
//获取数据
console.log(event.getData());
//获取来源
console.log(event.getSource();
}
})
//添加监听器
this.eventManager.addListener("onShow",this.myListener);
},
onShow: function() {
//发送数据
this.myEventManager.postMsg("app 显示了","onShow");
},
onHide: function() {
console.log('App Hide')
}
}
</script>
<style>
/*每个页面公共css */
</style>
24 个评论
要回复文章请先登录或注册
7***@qq.com
7***@qq.com
xxj_e_e
爱吃鱼的靖哥哥