0
点赞
收藏
分享

微信扫一扫

EventBus源码解析一,kotlin编程通俗演义pdf

晴儿成长记 2022-03-11 阅读 71
面试

}

}

}

return instance;

}

/**

  • 当我们调用 register 方法的时候,就把接收消息的方法放到 cacheMap 中

  • @param object

*/

public void register(Object object) {

List list = cacheMap.get(object);

if (list == null) {

list = findGsSubscribeMethods(object);

cacheMap.put(object, list);

}

}

private List findGsSubscribeMethods(Object object) {

List list = new ArrayList<>();

//获取 activity

Class<?> clazz = object.getClass();

// 除了获取当前activity的有添加注解的方法,它的父类中如果有添加注解的方法,也要放进list

while (clazz != null) {

// 过滤作用:凡是系统级别的父类,直接省略

String name = clazz.getName();

if (name.startsWith(“java.”) || name.startsWith(“javax.”) || name.startsWith(“android.”)) {

break;

}

//获取 activity 中的所有方法

Method[] methods = clazz.getDeclaredMethods();

for (Method method : methods) {

//找到带有 GsSubscribe 的方法

GsSubscribe gsSubscribe = method.getAnnotation(GsSubscribe.class);

//如果为空,我们就继续寻找下一个

if (gsSubscribe == null) {

continue;

}

//如果找到的话,我们就判断带有 GsSubscribe 注解的方法中的参数类型

Class<?>[] types = method.getParameterTypes();

//用以限制 传递事件的方法有且只有一个参数

if (types.length != 1) {

LogUtils.e(“GsEventBus only accept one parameter”);

}

// 判断线程类型

GsThreadMode gsThreadMode = gsSubscribe.gsThreadMode();

// 封装

GsSubscribeMethod gsSubscribeMethod = new GsSubscribeMethod(method, gsThreadMode, types[0]);

list.add(gsSubscribeMethod);

}

// 转到当前activity的父类中

clazz = clazz.getSuperclass();

}

return list;

}

public void unRegister(Object object) {

}

public void post(Object type) {

//直接循环 cacheMap,然后找到对应的方法进行回调

Set set = cacheMap.keySet();

Iterator iterator = set.iterator();

while (iterator.hasNext()) {

Object object = iterator.next();

//找到对应的activity中的含有注解的方法列表

List list = cacheMap.get(object);

for (GsSubscribeMethod gsSubscribeMethod : list) {

// 比对 post传递的参数对象与接收信息的方法的参数对象是否相同

if (gsSubscribeMethod.getType().isAssignableFrom(type.getClass())) {

// 判断在哪个线程

switch (gsSubscribeMethod.getmGsThreadMode()) {

case MAIN:

// 主线程 --> 主线程

if (Looper.myLooper() == Looper.getMainLooper()) {

invoke(gsSubscribeMethod, object, type);

}else { // 子线程 --> 主线程

mHandler.post(() ->

invoke(gsSubscribeMethod, object, type));

}

break;

case BACKGRUOND:

// 主线程 --> 子线程

// 子线程 --> 子线程

break;

}

}

}

}

}

private void invoke(GsSubscribeMethod gsSubscribeMethod, Object object, Object type) {

Method method = gsSubscribeMethod.getmMethod();

try {

method.invoke(object, type);

} catch (IllegalAccessException e) {

e.printStackTrace();

} catch (InvocationTargetException e) {

e.printStackTrace();

}

}

}

3.2、GsSubscribe


/**

  • Created by songzi522 on 2019/12/11.

  • 注解类,作用:一个标记,它标记的方法都将被GsEventBus收录

*/

// 表示注解的是一个方法

@Target(ElementType.METHOD)

// 表示它是运行时的注解

@Retention(RetentionPolicy.RUNTIME)

public @interface GsSubscribe {

GsThreadMode gsThreadMode() default GsThreadMode.MAIN;

}

3.3、GsSubscribeMethod.java


/**

  • Created by songzi522 on 2019/12/11.

  • 用来封装需要 GsEventBus 保存的方法

*/

public class GsSubscribeMethod {

//回调方法

private Method mMethod;

//线程模式

private GsThreadMode mGsThreadMode;

//回调方法中的参数类型

private Class<?> type;

public GsSubscribeMethod(Method mMethod, GsThreadMode mGsThreadMode, Class<?> type) {

this.mMethod = mMethod;

this.mGsThreadMode = mGsThreadMode;

this.type = type;

}

public Method getmMethod() {

return mMethod;

}

public void setmMethod(Method mMethod) {

this.mMethod = mMethod;

}

public GsThreadMode getmGsThreadMode() {

return mGsThreadMode;

}

public void setmGsThreadMode(GsThreadMode mGsThreadMode) {

this.mGsThreadMode = mGsThreadMode;

}

public Class<?> getType() {

return type;

}

public void setType(Class<?> type) {

this.type = type;

}

}

3.4、GsThreadMode


/**

  • Created by songzi522 on 2019/12/11.

  • 一个枚举类,它代表EventBus通过post发送消息时,接收的线程是在主线程还是子线程

*/

public enum GsThreadMode {

MAIN,

BACKGRUOND

}

3.5、EventBusTest4Activity.java


/**

  • 用来测试自己手写的 EventBus:GsEventBus

*/

public class EventBusTest4Activity extends BaseActivity {

@BindView(R.id.tvMessage)

TextView tvMessage;

@BindView(R.id.btn1)

Button btn1;

@BindView(R.id.btn2)

Button btn2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_event_bus_test4);

ButterKnife.bind(this);

GsEventBus.getDefault().register(this);

}

@OnClick({R.id.btn1, R.id.btn2})

public void onViewClicked(View view) {

switch (view.getId()) {

case R.id.btn1:

readyGo(EventBusTest5Activity.class);

break;

case R.id.btn2:

break;

}

}

//GsSubscribe 是个注解,它的作用是:只是一个标记,它标记的方法都将被 GsEventBus 收录

//GsThreadMode:一个枚举类,它代表 GsEventBus 通过post发送消息时,接收的线程是在主线程还是子线程

@GsSubscribe(gsThreadMode = GsThreadMode.MAIN)

public void getMessage(MyBusEvent event) {

LogUtils.e(event.message);

LogUtils.e(“当前线程名字:” + Thread.currentThread().getName());

}

}

3.6、EventBusTest5Activity.java


public class EventBusTest5Activity extends AppCompatActivity {

@BindView(R.id.tvMessage)

TextView tvMessage;

@BindView(R.id.btn1)

Button btn1;

写在最后

最后我想说:对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

上述【高清技术脑图】以及【配套的架构技术PDF】可以点击下面链接免费获取

Android学习PDF+架构视频+面试文档+源码笔记

适应环境,而不是环境来适应我们!**

这里附上上述的技术体系图相关的几十套腾讯、头条、阿里、美团等公司2021年的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

相信它会给大家带来很多收获:

[外链图片转存中…(img-YJgWDiFE-1646393438956)]

[外链图片转存中…(img-bTrOYgj2-1646393438958)]

上述【高清技术脑图】以及【配套的架构技术PDF】可以点击下面链接免费获取

Android学习PDF+架构视频+面试文档+源码笔记

举报

相关推荐

0 条评论