EventBus2和EventBus3的使用和原理介绍

EventBus的使用及实现原理 1.EventBus的介绍与使用 1.1常见的组件间的通信方式 Intent:跳转+传参(局限性非常大) Handler:通常用来更新主线程UI,使用不当容易出现内存泄露 Interface:接口回调,仅限于同一线程中数据交互 BroadcastReceiver:有序广播+无序广播 AIDL:跨进程通信,代码阅读性不友好,维护成本偏高 EventBus优点 代码简单,快 Jar包小,~50k... 阅读详情

EventBus已经发展到第三版了,对于它的使用和原理相对于之前也有了很大的改进。下面就介绍一下EventBus2和EventBus3的使用和原理
说明:方法上面的参数设置
threadMode:线程工作状态,表示方法在哪个线程执行
ThreadMode.POSTING:默认的设置,表示发送消息在哪个线程,就在哪个线程执行方法
ThreadMode.MAIN:主线程执行该方法
ThreadMode.MAIN_ORDERED:也是在主线程执行,只是不会阻塞主线程
ThreadMode.BACKGROUND:后台线程执行
ThreadMode.ASYNC:异步子线程

sticky:是否是粘性操作,表示如果方法在后面注册的话,也是可以收到消息的
priority:优先级,同一个消息,优先级高的优先收到消息

1、使用

1.1 EventBus2

1.1.1 EventBus2的配置

在app的build.gradle中进行配置

implementation 'org.greenrobot:eventbus:3.1.1'
annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
1.1.2 EventBus2的使用

注册:
需要在类中注册当前的对象,对象销毁的时候取消注册

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);//注册

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//取消注册
    }

	//定义事件
    @Subscribe(threadMode = ThreadMode.BACKGROUND,priority = 4)
    public void onEvent(int click){
        Log.e("MainActivity","onEvent"+click);
    }
}

发送事件:

EventBus.getDefault().post(3);
EventBus.getDefault().postSticky(new Eventtype(3));//发送粘性事件

发送完事件之后就可以在Logcat中发现方法onEvent中打印的Log了。

1.2 EventBus3

1.2.1 EventBus3的配置

(1)导包

implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'

(2)配置索引文件属性,在build.gradle中进行配置

  defaultConfig {
      javaCompileOptions{
            annotationProcessorOptions{
                includeCompileClasspath = true
                arguments = [
                        "eventBusIndex": "com.example.viewtest.EventBusIndex"]
            }
        }
        }

(3)定义方法
在类中定义方法

@Subscribe(threadMode = ThreadMode.BACKGROUND,priority = 4)
    public void onEvent(int click){
        Log.e("MainActivity","onEvent"+click);
    }

(4)编译
调用app的make project,就会生成对应的索引文件
在这里插入图片描述
(5)在application中进行注册

EventBus.builder().addIndex(new EventBusIndex()).installDefaultEventBus();
1.2.2 EventBus3的使用

基本操作都是一样的,也是需要注册和注销注册,发送事件

public class MainActivity extends AppCompatActivity {

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

        findViewById(R.id.tv_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new Eventtype(3));//发送消息
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND,priority = 4)
    public void onEvent(Eventtype click){
        Log.e("MainActivity","onEvent"+click.click);
    }
}

2、原理介绍

EventBus是实现跨线程通信的,那我们就要了解是如何实现跨线程的。
使用的模式就是观察者模式,先订阅消息,如果发生事件了,就回调订阅的方法。
下面我们就按照正常使用流程进行分析

2.1 订阅

使用的方法都是

EventBus.getDefault().register(this);

EventBus.getDefault()获取到EventBus单例对象。
注册的时候:

public void register(Object subscriber) {
        Class<?> subscriberClass = subscriber.getClass();
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
    }

subscriberMethodFinder中查找是否已经注册过了

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }

        if (ignoreGeneratedIndex) {
            subscriberMethods = findUsingReflection(subscriberClass);
        } else {
            subscriberMethods = findUsingInfo(subscriberClass);
        }
        if (subscriberMethods.isEmpty()) {
            throw new EventBusException("Subscriber " + subscriberClass
                    + " and its super classes have no public methods with the @Subscribe annotation");
        } else {
            METHOD_CACHE.put(subscriberClass, subscriberMethods);
            return subscriberMethods;
        }
    }
private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new ConcurrentHashMap<>();

METHOD_CACHE 是一个对象,用来存储类对象和对应的添加了注解的需要EventBus处理的订阅方法表。
后面的逻辑就是如果存储过了就不需要存储了,直接返回即可。如果没有存储过,就需要重新添加一遍,这里就存在两种方式,一种是findUsingReflection(subscriberClass),代表之前EventBus2.0的方式,就是通过反射查找到类中的添加了注解的方法,然后添加到表中;

private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            findUsingReflectionInSingleClass(findState);
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }

另一种就是findUsingInfo方法,EventBus3.0的方法,就是通过APT生成索引文件,然后通过索引文件查找到类中的添加了注解的方法,这样相对于反射速度更快,效率更高,并且索引文件是在编译时就生成的,这样不会影响运行时的效率,提升了性能。

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            findState.subscriberInfo = getSubscriberInfo(findState);
            if (findState.subscriberInfo != null) {
                SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
                for (SubscriberMethod subscriberMethod : array) {
                    if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
                        findState.subscriberMethods.add(subscriberMethod);
                    }
                }
            } else {
                findUsingReflectionInSingleClass(findState);
            }
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }

通过getSubscriberInfo()方法获取到索引文件中的注册信息

private SubscriberInfo getSubscriberInfo(FindState findState) {
        if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
            SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
            if (findState.clazz == superclassInfo.getSubscriberClass()) {
                return superclassInfo;
            }
        }
        if (subscriberInfoIndexes != null) {
            for (SubscriberInfoIndex index : subscriberInfoIndexes) {
                SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
                if (info != null) {
                    return info;
                }
            }
        }
        return null;
    }

这样就将所有注册的类中的添加了注解的方法存储完毕,等到发送消息时候就会遍历这个存储对象了。
当所有全部存储完之后,register方法中还需要走一步,就是调用订阅的方法

synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        Class<?> eventType = subscriberMethod.eventType;
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
        if (subscriptions == null) {
            subscriptions = new CopyOnWriteArrayList<>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
            if (subscriptions.contains(newSubscription)) {
                throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                        + eventType);
            }
        }

        int size = subscriptions.size();
        for (int i = 0; i <= size; i++) {
            if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
                subscriptions.add(i, newSubscription);
                break;
            }
        }

        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<>();
            typesBySubscriber.put(subscriber, subscribedEvents);
        }
        subscribedEvents.add(eventType);

        if (subscriberMethod.sticky) {
            if (eventInheritance) {
                // Existing sticky events of all subclasses of eventType have to be considered.
                // Note: Iterating over all events may be inefficient with lots of sticky events,
                // thus data structure should be changed to allow a more efficient lookup
                // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
                Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
                for (Map.Entry<Class<?>, Object> entry : entries) {
                    Class<?> candidateEventType = entry.getKey();
                    if (eventType.isAssignableFrom(candidateEventType)) {
                        Object stickyEvent = entry.getValue();
                        checkPostStickyEventToSubscription(newSubscription, stickyEvent);
                    }
                }
            } else {
                Object stickyEvent = stickyEvents.get(eventType);
                checkPostStickyEventToSubscription(newSubscription, stickyEvent);
            }
        }
    }

subscribe方法就是主要是三部分的内容,一是将订阅事件和对应的要执行的方法存储在subscriptionsByEventType(Map<Class<?>, CopyOnWriteArrayList<Subscription>>对象)中,存储链表的时候,还需要参考优先级,优先级高的放在前面;二是将类对象的注解方法参数添加到typesBySubscriber(Map<Object, List<Class<?>>>)中,确认该类可以接收哪几种消息;三是粘性事件处理,如果当前方法是粘性的,就需要注解方法参数是否和之前的参数一致,如果一致那么就需要执行该方法。

2.2 发送消息

EventBus.getDefault().post(new Eventtype(3));

发送消息统一的方法就是调用EventBus的post方法,传递一个事件的对象。

public void post(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
        eventQueue.add(event);

        if (!postingState.isPosting) {
            postingState.isMainThread = isMainThread();
            postingState.isPosting = true;
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
                while (!eventQueue.isEmpty()) {
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }

上面方法的逻辑就是首先将消息添加到事件处理队列中,然后使用while 循环,调用postSingleEvent方法处理消息。

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            for (int h = 0; h < countTypes; h++) {
                Class<?> clazz = eventTypes.get(h);
                subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
            }
        } else {
            subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
        }
        if (!subscriptionFound) {
            if (logNoSubscriberMessages) {
                logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
            }
            if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
                    eventClass != SubscriberExceptionEvent.class) {
                post(new NoSubscriberEvent(this, event));
            }
        }
    }

lookupAllEventTypes()方法是用来查找所有的需要处理的事件类型,包括传入的EventType、他的父类和接口。找到类之后,postSingleEventForEventType方法查找对应的事件方法

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> subscriptions;
        synchronized (this) {
            subscriptions = subscriptionsByEventType.get(eventClass);
        }
        if (subscriptions != null && !subscriptions.isEmpty()) {
            for (Subscription subscription : subscriptions) {
                postingState.event = event;
                postingState.subscription = subscription;
                boolean aborted = false;
                try {
                    postToSubscription(subscription, event, postingState.isMainThread);
                    aborted = postingState.canceled;
                } finally {
                    postingState.event = null;
                    postingState.subscription = null;
                    postingState.canceled = false;
                }
                if (aborted) {
                    break;
                }
            }
            return true;
        }
        return false;
    }

通过subscriptionsByEventType查找到对应的事件链表,执行链表中的每个方法,也就是调用 postToSubscription(subscription, event, postingState.isMainThread);方法,后面就是涉及跨线程通信的过程了。

2.3 跨线程通信

在postToSubscription方法中,是最后跨线程通信的部分,源码如下:

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case POSTING:
                invokeSubscriber(subscription, event);
                break;
            case MAIN:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case MAIN_ORDERED:
                if (mainThreadPoster != null) {
                    mainThreadPoster.enqueue(subscription, event);
                } else {
                    // temporary: technically not correct as poster not decoupled from subscriber
                    invokeSubscriber(subscription, event);
                }
                break;
            case BACKGROUND:
                if (isMainThread) {
                    backgroundPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
            case ASYNC:
                asyncPoster.enqueue(subscription, event);
                break;
            default:
                throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
        }
    }

上面方法就是通过方法上面设置的threadMode参数,将方法放在哪个线程执行。一是默认当前线程,就直接通过反射执行该方法了;二是主线程,如果但是主线程,就直接执行该方法,如果不是,那就通过Handler发送到主线程进行执行;三也是主线程,同样的逻辑;四是后台线程,如果当前是主线程,那就将方法发送到后台线程队列中,然后通过EventBus线程池进行执行,如果当前不是主线程,那就直接执行;五是异步子线,直接发送到EventBus的线程池中进行执行。
说明:EventBus线程池是一种常用的缓存线程池,所以有消息的时候就会立刻进行处理。

private final static ExecutorService DEFAULT_EXECUTOR_SERVICE = Executors.newCachedThreadPool();

到这里整个流程基本分析完毕了,没怎么讲的就是EventBus3中的索引文件生成过程,这个就是通过APT,编译时生成文件,现在基本大多数的框架都使用了这个框架,包括ARouter、Retrofit,这样有助于提高效率,避免使用反射。

XM2BUS二总线芯片XMS110数据手册V1.0 XMS110是二总线技术中的一种主从模式从站通讯芯片,配合XM2BUS主站/中继使用。 XM2BUS属于直流低压二总线通信技术;通过对供电电缆上调制电压信号,并解调电流信号,进行通信,替代传统通信与供电分离通信方式。不仅提高了稳定性,并解决现场布线施工问题。 XMS110能够支持总线任意方式布线,如星型、树形、总线型等。 XMS110采用集成芯片,精简外围电路,设计极为简单,广泛应用于消防、工业控制、智能建筑、仪表等领域。 立即下载

相关推荐

S32K344 Bootloader实战:一个按键实现从Boot到App的无缝跳转(附完整代码)

本文详细介绍了基于NXP S32K344微控制器的Bootloader开发实战,重点解析如何通过一个硬件按键实现从Boot到App的可靠跳转。内容涵盖内存分区规划、中断向量表重映射、外设初始状态管理等核心要素,并提供了完整的代码示例和调试技巧,适用于汽车电子和工业控制等高可靠性应用场景。

weixin_29192365的博客 307

EventBus 2.x升级到3.0使用注意

https://www.jianshu.com/p/9ceb2f18b629

病毒才俊 691

用74LS194和555定时器DIY流水灯:一个经典的数字电路课程设计复盘(附Multisim仿真文件)

本文详细介绍了使用74LS194移位寄存器和555定时器搭建八路彩灯控制器的完整过程,包括硬件选型、电路设计、调试技巧及常见问题解决方案。作为经典的数字电路课程设计,该项目涵盖了EDA工具应用、电路原理图设计和Multisim仿真等核心技能,适合电子工程学习者实践参考。

weixin_29185199的博客 425

EventBus 源码分析(上篇)

可用于应用内的消息事件传递,方便快捷,耦合性低 1.基本用法 public class EventBusMain extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedIns...

weixin_34405332的博客 206

一文搞定学术英语写作 (斯坦福SCI论文写作课程笔记)

0.前言: 本文基于斯坦福Kristin Sainani 教授的学术论文写作公开课——SCI论文写作(Writing in the Sciences)。在此基础上进行章节顺序的重排及部分内容的删减,以方便第一次进行英语学术论文写作的同学快速上手。 时间充足和想要系统学习英文学术写作的同学,十分建议观看完整课程视频,一定会受益匪浅!课程地址如下: 课程B站地址:https://www.bilibili.com/video/BV1zv41177JQ 1.论...

Ogmx的博客 3万+

28_Spring AI 干货笔记之 OpenAI 聊天功能

本文详细介绍了在Spring AI框架中集成OpenAI聊天模型的方法。内容涵盖API密钥配置、通过starter依赖启用自动配置、连接与模型参数的详细设置,并重点说明了运行时选项的动态覆盖机制。文章还深入讲解了多模态处理(图像与音频)、结构化输出、函数调用等高级功能,提供了示例控制器和手动配置指南,并讨论了与OpenAI兼容服务器的适配与推理模型思维链的获取。

在科技的浪潮中,我们寻找着创新的火种,在代码的海洋里,我们编织着智慧的网。腾飞开源,就是这样一个由技术精英汇聚而成的博客平台,我们致力于分享在Java、Python、IoT和人工智能等领域的最新研究成果和实战经验。 在腾飞开源的博客上,你会看到紧跟技术前 1655

EventBus3基本使用与进阶配置

介绍 EventBus是适用于Android和Java的发布/订阅事件总线。可简化活动,片段,线程,服务等之间的通信。代码更少,质量更高。 基本使用 项目配置EventBus依赖 implementation 'org.greenrobot:eventbus:3.2.0' 定义事件 public class MessageEvent { } 注册/解除订阅者和定义接受事件的方法,在Android中应该根据组件生命周期进行注册/解注册 @Override public void on

李林杰的博客 1589

天地图下载带审图号(审图号: GS(2024)0650号)的 “行政区划” 数据(含shp文件下载地址)

在科研投稿论文过程中,多数期刊杂志要求我们制作涉及国界的图件,必须使用自然资源部地图技术审查中心发布的标准地图底图。如地理学报、地理科学等期刊的投稿指南中写到:非标准底图本刊不予发表!(如图1.1所示)如果一定要用,凡涉国界图件(国内部分地区、全国、世界部分地区、全球)必须使用自然资源部标准地图服务系统标准地图底图(http://bzdt.ch.mnr.gov.cn/)(如图1.1所示)或国家测绘局地理信息局标准地图底图(如图1.2所示),所用底图边界要完全无修改(包括南海诸岛位置)。

JData_Engineer的博客 2302

EventBus2.0-3.0的简单使用与详解

EventBus是一个用于简化Andorid、Fragment、Threads、Service之间信息传递的一个发布/订阅事件集。 EventBus是由大名鼎鼎的greenrobot出品的一个用于Android中事件发布/订阅的库。简单点说就是用于Fragment,Activity,Service,线程之间进行数据传递,它为开发者提供除了 intent、handler、boardcast这几种传

qq18811561684的博客 1828

6.4 直流无刷电机(BLDC):测试与性能评估

本文系统阐述了直流无刷电机(BLDC)的测试与性能评估方法,涵盖参数测量、性能测试、测试平台设计及可靠性评估。详细介绍了绕组电阻、电感和反电动势的测量原理与修正方法,以及转矩-转速特性、效率分析和动态响应测试的技术要点。通过搭建高精度测功机系统,结合自动化数据处理,实现对电机性能的全面评估。最后讨论了温升、振动、EMC等特殊测试方法,为BLDC电机的设计验证和质量控制提供了完整的技术路径。

技术工作者 439

Ardupilot EKF3核心算法《开篇》

Ardupilot EKF3核心算法本专栏重点,透彻,详细的分析了ardupilot的EKF3,保证是你看到的最完美的EKF3资料,相信你的选择不会错误。网上EKF资料真是太多,说真的没有一篇讲解好的。!!

陌城烟雨 1413

Android--EventBus的使用介绍

EventBus使用详解EventBus是什么? EventBus是一种发布者和订阅者的事件分发模式。我们可以这么理解,比如我们订阅的Android Weekly,每当星期一0点这个时候,Android WeekLy公司都会以邮件的形式通知我们。这里有四个概念 1.发布者-事件的来源者,通过它进行通知接收者或者观察者,如我们的Android Weekly 2.接收者-监听事件的对

qq_孤小狼的博客 412

Redis下载安装与配置(windows)

一、Redis下载 二、Redis安装 三、启动Redis临时服务并测试连接(可跳过) 四、Redis配置

欢迎订阅我的“JAVA全栈开发笔记(全)”专栏,价格优惠,仅需19.9元。 7万+

EventBus的基本使用及原理

一、EventBus的作用          学习使用之前我们需要知道EventBus是干什么的,通俗的说就是activity与activity,fragment与fragment,actvitiy与fragment,activity与service,fragment与service等之间的通讯,这些通讯我们可以只是使用BroadcastReceiver来实现,但是使用BroadcastRece

qq_34895850的博客 687

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

可能你的EventBus使用并不正确,是时候真正搞懂EventBus了(上)

EventBus作为Android开发中使用频率相当高的项目,帮助我们简化了很多复杂场景下的数据传递,当然也是Android初中高级面试中必不可少的问题。 但是对于EventBus使用,可能绝大多数人并没有正确的使用它,今天就让我来带大家正确的使用它,并带大家从源代码层面真正搞懂EventBus。 EventBus项目地址: https://github.com/greenrobot/EventBus 1、EventBus使用 第一步:注册 //注册 EventBus.getDefault().reg

步行者 3476

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

浅析EventBus

绿巨人的两个知名开源库EventBus 和GreenDao用了多年,今天闲暇总结下EventBus。 1 Why 官网给出解释 EventBus... simplifies the communication between components decouples event senders and receivers performs well with Activities, Fragments, and background threads avoids complex and e

神秘岛 293

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

Android -- 从源码带你从EventBus2.0飚到EventBus3.0

1,最近看了不少的面试题,不管是百度、网易、阿里的面试题,都会问到EventBus源码和RxJava源码,而自己只是在项目中使用过,却没有去用心的了解它底层是怎么实现的,所以今天就和大家一起来学习学习 2,简介EventBus2.x EventBus是用来干什么的?   EventBus是针一款对Android的发布/订阅事件总线。它可以让我们很轻松的实现在Android各个组件之间传递消息,...

weixin_33968104的博客 179

Android 百度面试Evenbus3.0源码解析 3分钟手写最牛框架 看完成大神

1.Evenbus的新旧版本使用区别 2.Evenbus源码解读之注解框架 3.Evenbus源码解读之索引用法 4.Evenbus源码解读之粘性事件 实现 5.Evenbus源码解读之不同线程通信 新版本和旧版本的区别: 接收到消息之后的处理方式,在2.x版本中,注册这些消息的监听需要区分是否监听黏性(sticky)事件,监听EventBus事件的模块需要实...

深南大盗的博客 1259

android-eventbus-使用详解

EventBus是什么? EventBus是一种发布者和订阅者的事件分发模式。我们可以这么理解,比如我们订阅的Android Weekly,每当星期一0点这个时候,Android WeekLy公司 都会以邮件的形式通知我们。 这里有四个概念 : 1. 发布者 - 事件的来源者,通过它进行通知接收者或者观察者,如我们的Android Weekly  2. 接收者 - 监听事件的对象,比如

Gx_1013的博客 639

vue3.2 组件之间的数据共享方式(含vuex详解)

vue3组件通信的几种方式

ailsa_csdn的博客 4927

Android EventBus 的使用介绍与原理

EventBus 的使用介绍与原理

OuO-1 的博客 298

EventBus原理源码分析和使用

有段时间被别人问到关于EventBus的原理,但我确实使用过,用起来也方便简单,一个注册,一个Post就能通知到所有的订阅者Subcriber,其实明明是知道一点点原理的,但是当时就是支支吾吾没有说,太low了,回来大概看了一眼EventBus的源码及实现原谅,现在想把看原代码的一些心得进行记录。 OK,我们从Github上面了解一下官方如何介绍这个EventBus吧。 翻

蹦跶的小码哥 2万+

一文彻底了解EventBus3原理

1.简介 EventBus是一种用于Android的事件发布-订阅总线框架,由GreenRobot开发,Gihub地址是:EventBus。它简化了应用程序内各个组件之间进行通信的复杂度,尤其是碎片之间进行通信的问题,可以避免由于使用广播通信而带来的诸多不便。 2.Subscribe注解 自3.0开始,订阅事件的方法开始使用了Subscribe注解,不再使用方法名了,如以下方式 @Subscri...

刘洋巴金的专栏 1万+

EventBus的使用与实现原理

的

SpringQi 1808

Eventbus 使用方法和原理分析

对于 Eventbus ,相信很多 Android 小伙伴都用到过。 1、创建事件实体类 所谓的事件实体类,就是传递的事件,一个组件向另一个组件发送的信息可以储存在一个类中,该类就是一个事件,会被 EventBus 发送给订阅者。新建 MessageEvent.java: public class MessageEvent { private String m...

weixin_30682127的博客 168

java EventBus 使用总结

EventBus是一个典型的事件分发器,Observer模式。订阅者通过register集中到EventBus类中,当发布者通过post MessageEvent时,通知到订阅者。适用于一对多,当一个消息需要被多次不同处理时使用。使用 EventBus 进行注册订阅者,发送事件,可以实现所有订阅者都收到了发送的事件,就是一个 发布/订阅 模式。EventBus 还有个实现类:AsyncEventBus。

javatuo的博客 3243
上一篇: Android View分析(五)—— 动画介绍
下一篇: Android 埋点解决方案
明天的增加
博客等级 码龄11年 22粉丝 71原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值