Android精准发送Event事件响应框架(解决滥用Event
在Android项目中,相信Eventbus大家已经很熟悉了,Eventbus是一个支持跨Module的事件发送响应框架,但是随着项目的不断扩大 以及多人团队的开发,渐渐的会出现一个不大和谐的情况,就是有一部分开发童鞋由于一些特定的业务场景,会经常复用组内其他成员的event事件,久而久之 就会出现多个eventbus事件发送 多个场景同时接收,某天也许你会发现一个很奇怪的bug,但怎么也找不到原因,最后会发现是之前的童鞋滥用的eventbus导致,这类的bug往往要浪费很长时间,而且有的event事件也找不到post源头和handle的接受者。
应对这种情况,本人开发了点对点的开发框架,大体保持了EventBus的用法,以下是使用方式:
接收:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PointBus.getDefault().register(this);
setContentView(R.layout.activity_main);
}
public void onTestClick(View view){
Intent intent = new Intent(this, TestActivity.class);
startActivity(intent);
}
@PointSubscribe(threadMode = ThreadMode.MAIN)
public void handleEvent(TestEvent testEvent) {
Toast.makeText(this,testEvent.msg,Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
PointBus.getDefault().unRegister(this);
}
发送:
public void testPost() {
PointBus.getDefault().post(this, new TestEvent("from TestActivity"), "com.fly.ui.test.MainActivity");
}
这里的com.fly.ui.test.MainActivity就是上述接收事件的Activity 就算其他的Actvity或者Fragment 处理的相同的event 也接收不到 你可以指定你的event 往哪发松
下面是实现原理,大体原理还是使用的APT 这里对APT不熟悉的童鞋可以找些基础的文章学习一下 很容易上手 下面贴一下process部分关键的代码
TypeElement classElement = (TypeElement) method.getEnclosingElement();
VariableElement param = method.getParameters().get(0);
TypeMirror typeMirror = getParamTypeMirror(param, messager);
String className = classElement.getSimpleName().toString();
if (element instanceof ExecutableElement) {
PointSubscribe bindView = element.getAnnotation(PointSubscribe.class);
String threadMode = bindView.threadMode().name();
ExecutableElement method = (ExecutableElement) element;
这样明白了注解标注的方法对应的类的全名和event类全名 所有的信息都存储在缓存列表中
最后创建一个Java文件
Writer writer = jfo.openWriter();
writer.write("package " + packName + ";");
writer.write("\n\n");
writer.write("import com.fly.bus.SubscribeImpl;");
writer.write("\n");
writer.write("import com.fly.bus.SubscribeInfo;");
writer.write("\n");
writer.write("import java.util.ArrayList;");
writer.write("\n");
writer.write("import java.util.HashMap;");
writer.write("\n");
writer.write("import java.util.List;");
writer.write("\n");
writer.write("public class " + newClassName + " implements SubscribeImpl{\n\n");
writer.write(" public static HashMap<String,List<SubscribeInfo>> subscribeInfoMap;\n");
writer.write(" public static List<SubscribeInfo> subscribeInfoList;\n");
writer.write(" static {\n");
writer.write(" subscribeInfoMap = new HashMap<>();\n");
initSubscribeInfo(writer, hashMap);
writer.write(" }\n");
writer.write(" public static void initSubscribeInfo(String methodName,String eventName,String threadMode){\n");
writer.write(" subscribeInfoList.add(new SubscribeInfo(methodName,eventName,threadMode)); \n");
writer.write(" }\n");
writer.write(" @Override\n");
writer.write(" public HashMap<String, List<SubscribeInfo>> getSubscribeInfo(){\n");
writer.write(" return subscribeInfoMap;\n");
writer.write(" }\n");
writer.write("\n\n");
writer.write("}\n");
writer.flush();
writer.close();
private void initSubscribeInfo(Writer writer, HashMap<String, List<SimpleSubscribeInfo>> hashMap) {
try {
for (String className : hashMap.keySet()) {
writer.write(" subscribeInfoList = new ArrayList<>();\n");
List<SimpleSubscribeInfo> infoList = hashMap.get(className);
if (infoList == null || infoList.size() == 0) {
continue;
}
for (SimpleSubscribeInfo info : infoList) {
String methodName = info.getMethodName();
String eventName = info.getEventName();
String threadMode = info.getThreadMode();
writer.write(" initSubscribeInfo(" + "\"" + methodName + "\"" + "," + "\"" + eventName + "\"" + "," + "\"" + threadMode + "\"" + "); \n");
}
writer.write(" subscribeInfoMap.put(" + "\"" + className + "\"" + ",subscribeInfoList);\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
总结:将所有注解的方法对应的类和参数名 在编译器 利用AbstractProcessor保存在一个在编译器生成的Hashmap里,当你post的时候 以你的目标类的全名(com.fly.ui.test.MainActivity)作为key去寻找出对应的value,value里保存着event类全名和方法名 以及要处理事件的线程,然后就invoke method即可
最后附上github链接:https://github.com/tmacfly/PointBusMaster
转发请标明出处 万分感谢