手写EventBus

作者: 猫叔很不爽 | 来源:发表于2020-03-14 15:54 被阅读0次
public class MyEventBus {

    private static MyEventBus instance = new MyEventBus();
    private Map<Object, List<SubscribleMethod>> cacheMap;
    private Handler mHandler;
    private ExecutorService executorService;

    private MyEventBus(){
        cacheMap = new HashMap<>();
        // 主进程Handler
        mHandler = new Handler(Looper.getMainLooper());
        // 执行子任务的线程池
        executorService = Executors.newCachedThreadPool();
    }

    public static MyEventBus getDefault(){
        return instance;
    }

    /**
     *  注册接收事件的类
     * @param subscriber MainActivity
     */
    public void register(Object subscriber){
        Class<?> subscriberClass = subscriber.getClass();
        List<SubscribleMethod> subscribleMethods = cacheMap.get(subscriberClass);
        // 等于空就表示这个subscriber没有注册到cacheMap中
        if(subscribleMethods == null){
            subscribleMethods = getMethodsFromSubscriber(subscriber);
            cacheMap.put(subscriber,subscribleMethods);
        }
    }

    // 寻找订阅者(MainActivity)中的订阅方法。即打了Subscrible注解的方法
    private List<SubscribleMethod> getMethodsFromSubscriber(Object subscriber) {
        List<SubscribleMethod> subscribleMethodList = new ArrayList<>();
        Class<?> subscriberClass = subscriber.getClass();
        // Eventbus也许是注册在BaseActivity中的,所以要父类也需要遍历。
        while (subscriberClass != null){
            String name = subscriberClass.getName();
            // BaseActiviy的父类是Activity,Activity肯定是不需要遍历的,Activity的包名以Android.开头,所以遍历到这里,就break
            if(name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.") || name.startsWith("androidx.")){
                break;
            }
            Method[] methods = subscriberClass.getDeclaredMethods();
            // 寻找和 EventBus 相关的方法,并保存
            for(int i = 0; i < methods.length; i++){
                //  EventBus语法,只能接收一个参数。
                if(methods[i].getParameterTypes().length != 1){
                    continue;
                }
                // 必须要有 MySubscribe 注解,才是 EventBus 需要的方法
                MySubscribe annotation = methods[i].getAnnotation(MySubscribe.class);
                if(annotation == null){
                    continue;
                }
                MyThreadMode myThreadMode = annotation.threadMode();
                Class<?> eventType = methods[i].getParameterTypes()[0]; // 只有一个参数,所以获取第0个位置上的数据
                SubscribleMethod method = new SubscribleMethod(methods[i],myThreadMode,eventType);
                // 将符合条件的方法保存到List集合中,并返回
                subscribleMethodList.add(method);
            }
            subscriberClass = subscriberClass.getSuperclass();
        }

        return subscribleMethodList;
    }

    /**
     *  发送事件
     * @param event  MessageEvent
     */
    public void post(Object event){
        Set<Object> keySet = cacheMap.keySet();
        Iterator<Object> iterator = keySet.iterator();
        // 遍历所有的注册类,在他们的注册事件中寻找类型一样的方法,利用反射执行这些方法
        while(iterator.hasNext()){
            // 注册类 (MainActivity)
            Object next = iterator.next();
            List<SubscribleMethod> subscribleMethodList = cacheMap.get(next);
            for(int i = 0; i < subscribleMethodList.size(); i++){
                SubscribleMethod method = subscribleMethodList.get(i);
                if(method.getEventType().isAssignableFrom(event.getClass())){
                    try{
                        invoke(method,next,event);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    // 使用反射执行
    private void invoke(final SubscribleMethod method,final Object next,final Object event) throws Exception{
            // 注册事件 指定的线程
        try{
            MyThreadMode threadMode = method.getThreadMode();
            if(threadMode == MyThreadMode.MAIN){
                // 发送事件如果是主线程
                if(Looper.myLooper() == Looper.getMainLooper()){
                    // 主线程发送,主线程接收
                    method.getMethod().invoke(next,event);
                }else{
                    // 发送事件是子线程,使用Handler发送消息
                    // 子线程发送,主线程接收
                    mHandler.post(new Runnable(){
                        @Override
                        public void run() {
                            try{
                                method.getMethod().invoke(next,event);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }else if(threadMode == MyThreadMode.ASYNC){
                if(Looper.myLooper() == Looper.getMainLooper()){
                    // 使用线程池,创建任务异步执行
                    // 主线程发送,子线程接收
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            try{
                                method.getMethod().invoke(next,event);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    });
                }else {
                    // 子线程发送,子线程接收
                    method.getMethod().invoke(next,event);
                }
            }else{
                method.getMethod().invoke(next,event);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     *  解除订阅
     */
    public void unregister(Object subscriber){
        List<SubscribleMethod> subscribleMethodList = cacheMap.get(subscriber);
        if(subscribleMethodList != null){
            cacheMap.remove(subscriber);
        }
    }
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface MySubscribe {
    MyThreadMode threadMode() default MyThreadMode.POSTING;
}
// 注册类中的注册方法信息
public class SubscribleMethod {

    // 注册方法
    private Method method;

    // 线程类型
    private MyThreadMode threadMode;

    // 事件类型
    private Class<?> eventType;

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

    public Method getMethod() {
        return method;
    }

    public MyThreadMode getThreadMode() {
        return threadMode;
    }

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

    // 谁发送的事件就在谁的线程中执行
    POSTING,
    // 在主线程中执行
    MAIN,
    // 在子线程中执行
    ASYNC

}

测试代码

public class MainActivity extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyEventBus.getDefault().register(this);

        tv = findViewById(R.id.tv);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                MainActivity.this.startActivity(intent);
            }
        });
    }

    @MySubscribe(threadMode = MyThreadMode.MAIN)
    public void get(MessageEvent event){
        Log.e("xsl", event.getStr() );
        tv.setText(event.getStr());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyEventBus.getDefault().unregister(this);
    }
}
public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
    }

    public void send(View view){
        new Thread(new Runnable() {
            @Override
            public void run() {
                MyEventBus.getDefault().post(new MessageEvent("我有一个小毛驴,我一直都不骑!"));
            }
        }).start();
    }
}

结果

image.png

<https://github.com/xslandlr/AndroidStudy>
** MyEventBus **

相关文章

网友评论

    本文标题:手写EventBus

    本文链接:https://www.haomeiwen.com/subject/abjwshtx.html