Android开发之EventBus3.1.1的源码简单分析以及手动实现自己的EventBus

前言:在上一个章节中《Kotlin-Android开发之透彻理解EventBus的MAIN、POSTING、BACKGROUND、ASYNC,4种线程模式》介绍了EventBus的使用方法以及EventBus的4种线程模式,其实在开发中会使用就行了,如果你有更高的追求,那就需要来翻阅一下源码了,我也是现学现卖,如有不周到的地方,还请指出。


源码解析:

1.EventBus的好处:EventBus可以代替Android传统的Intent,Handler,Broadcast或接口函数,在Fragment,Activity,Service线程之间传递数据,执行方法。

2.EventBus.getDefault():获取EventBus的单例对象。部分源码截图

3.register :根据当前的类进行注册(也就是订阅者),然后根据订阅者去缓存中去查找(使用findSubscriberMethods方法),如果能找到直接返回subscriber这个类的所有订阅方法,反之就添加进缓存并且返回这个储存了所有订阅函数的集合subscriberMethods,最后通过for循环遍历通过subscribe方法把subscriber和subscriberMethod添加到map集合。

部分源码截图:

register流程图:

4.post:根据currentPostingThreadState.get()方法获取事件类型,根据类型通过postSingleEvent()和postSingleEventForEventType()获取订阅者和订阅方法,然后根据postToSubscription()方法根据是否在主线程和threadmode来进行判断,最后通过反射直接调用订阅者的订阅方法完成本次通信(invokeSubscriber())。

部分源码截图:

post流程图:

5.unregister:先订阅者从typesBySubscriber中获取到所有的事件类型,然后有了事件类型列表就可以遍历事件类型从subscriptionsByEventType获取到对应的订阅者包括订阅函数来释放,最后根据订阅者删除掉typesBySubscriber中的事件类型 此时订阅者已经被释放不再持有该订阅者引用

部分源码截图:

6.threadMode:

1)MAIN:主线程调用,视发布线程不同处理不同,如果发布者在主线程那么直接调用(非阻塞式),如果发布者不在主线程那么阻塞式调用。子线程发布->主线程接收,主线程发布->主线程接收

2)POSTING:默认的模式,开销最小的模式,因为声明为POSTING的订阅者会在发布的同一个线程调用,发布者在主线程那么订阅者也就在主线程,反之亦,避免了线程切换,如果不确定是否有耗时操作,谨慎使用,因为可能是在主线程发布。子线程发布->子线程接收,主线程发布->主线程接收

3)BACKGROUND:在子线程调用,如果发布在子线程那么直接在发布线程调用,如果发布在主线程那么将开启一个子线程来调用,这个子线程是阻塞式的,按顺序交付所有事件,所以也不适合做耗时任务,因为多个事件共用这一个后台线程。主线程发布->子线程接收、子线程发布->子线程接收

4)ASYNC:在子线程调用,总是开启一个新的线程来调用,适用于做耗时任务,比如数据库操作,网络请求等,不适合做计算任务,会导致开启大量线程。主线程发布->创建新的子线程接收、子线程发布->创建新的子线程接收(两个子线程不同)

7.Subscribe:@Subscribe采用运行时注解,且注解只能用在函数上,默认的threadmode为posting。


仿照EventBus手动实现自己的EventBus:

FlyEventBus.java
import android.os.Handler;
import android.os.Looper;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FlyEventBus {
    private static FlyEventBus instance = new FlyEventBus();
    private Map<Object, List<SubscribleMethod>> cacheMap;
    private Handler handler;

    //线程池
    private ExecutorService executorService;

    public static FlyEventBus getDefault() {
        return instance;
    }

    private FlyEventBus() {
        this.cacheMap = new HashMap<>();
        handler = new Handler(Looper.getMainLooper());
        executorService = Executors.newCachedThreadPool();
    }

    //注册
    public void register(Object subscriber) {
        Class<?> aClass = subscriber.getClass();
        List<SubscribleMethod> subscribleMethods = cacheMap.get(subscriber);
        //如果已经注册,就不需要注册
        if (subscribleMethods == null) {
            subscribleMethods = getSubscribleMethods(subscriber);
            cacheMap.put(subscriber, subscribleMethods);
        }
    }

    //遍历能够接收事件的方法
    private List<SubscribleMethod> getSubscribleMethods(Object subscriber) {
        List<SubscribleMethod> list = new ArrayList<>();
        Class<?> aClass = subscriber.getClass();
        //需要 subscriber --- 》BaseActivity    ------->Activitiy
        while (aClass != null) {
            //判断分类是在那个报下,(如果是系统的就不需要)
            String name = aClass.getName();
            if (name.startsWith("java.") ||
                    name.startsWith("javax.") ||
                    name.startsWith("android.") ||
                    name.startsWith("androidx.")) {
                break;
            }

            Method[] declaredMethods = aClass.getDeclaredMethods();
            for (Method method : declaredMethods) {
                FlySubscribe annotation = method.getAnnotation(FlySubscribe.class);
                if (annotation == null) {
                    continue;
                }

                //检测这个方法合不合格
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length != 1) {
                    throw new RuntimeException("eventbusz只能接收一个参数");
                }

                //符合要求
                FlyThreadMode flyThreadMode = annotation.threadMode();
                SubscribleMethod subscribleMethod = new SubscribleMethod(method, flyThreadMode, parameterTypes[0]);
                list.add(subscribleMethod);
            }
            aClass = aClass.getSuperclass();
        }
        return list;
    }


    //取消注册
    public void unregister(Object subscriber) {
        Class<?> aClass = subscriber.getClass();
        List<SubscribleMethod> list = cacheMap.get(subscriber);
        //如果获取到
        if (list != null) {
            cacheMap.remove(subscriber);
        }
    }

    public void post(final Object obj) {
        Set<Object> set = cacheMap.keySet();
        Iterator<Object> iterator = set.iterator();
        while (iterator.hasNext()) {
            //拿到注册类
            final Object next = iterator.next();

            //获取类中所有添加注解的方法
            List<SubscribleMethod> list = cacheMap.get(next);
            for (final SubscribleMethod subscribleMethod : list) {
                //判断这个方法是否应该接收事件
                if (subscribleMethod.getEventType().isAssignableFrom(obj.getClass())) {
                    switch (subscribleMethod.getThreadMode()) {
                        case MAIN:
                            //如果接收方法在主线程执行的情况
                            if(Looper.myLooper() == Looper.getMainLooper()){
                                invoke(subscribleMethod, next, obj);
                            } else {
                                //post方法执行在子线程中,接收消息在主线程中
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        invoke(subscribleMethod, next, obj);
                                    }
                                });
                            }
                            break;
                        //接收方法在子线程种情况
                        case ASYNC:
                            //post方法执行在主线程中
                            if(Looper.myLooper() == Looper.getMainLooper()){
                                executorService.execute(new Runnable() {
                                    @Override
                                    public void run() {
                                        invoke(subscribleMethod, next, obj);
                                    }
                                });
                            } else {
                                //post方法执行在子线程中
                                invoke(subscribleMethod, next, obj);
                            }
                            break;
                    }

                }
            }
        }
    }

    private void invoke(SubscribleMethod subscribleMethod, Object next, Object obj) {
        Method method = subscribleMethod.getMethod();
        try {
            method.invoke(next, obj);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
FlySubscribe.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FlySubscribe {
    FlyThreadMode threadMode() default FlyThreadMode.POSTING;
}
FlyThreadMode.java
public enum FlyThreadMode {

    POSTING,
    MAIN,
    MAIN_ORDERED,
    BACKGROUND,
    ASYNC
}
SubscribleMethod.java
import java.lang.reflect.Method;

//注册类中的注册方法信息
public class SubscribleMethod {
    //注册方法
    private Method method;
    //线程类型
    private FlyThreadMode threadMode;

    //参数类型
    private Class<?> eventType;

    public SubscribleMethod(Method method, FlyThreadMode threadMode, Class<?> eventType) {
        this.method = method;
        this.threadMode = threadMode;
        this.eventType = eventType;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public FlyThreadMode getThreadMode() {
        return threadMode;
    }

    public void setThreadMode(FlyThreadMode threadMode) {
        this.threadMode = threadMode;
    }

    public Class<?> getEventType() {
        return eventType;
    }

    public void setEventType(Class<?> eventType) {
        this.eventType = eventType;
    }
}

总结:由此可以得出EventBus底层采用的是注解和反射的方式来获取订阅方法信息(首先是注解获取,若注解获取不到,再用反射),当前订阅者是添加到Eventbus总的事件订阅者的subscriptionByEventType集合中。

发布了117 篇原创文章 · 获赞 80 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_32306361/article/details/104279443