zoukankan      html  css  js  c++  java
  • EventBus

    我的EventBus理解:

    1.EventBus概念
    Eventbus是一个事件发布和订阅的处理框架

    1.EventBus如何获取呢?

    在app模块下的build.gradle文件依赖项中添加如下
    compile 'org.greenrobot:eventbus:3.0.0'

    EventBus的基本使用

    1.新建一个事件类型 EventMessage
    2.注册当前对象为事件订阅者
    EventBus.getDefault().register(this);

    3.新建一个订阅者事件处理函数

    @Subscribe
    pubic void HandleEvent(Event Message){
    处理
    ]


    4.不用的时候要反注册 取消订阅
    EventBus.getDefault().unRegister(this);


    订阅者函数所在的线程模型

    Main 接受者方法是在主线程中执行

    Posting 接受者方法执行的线程和发布者是在同一线程

    Aysnc  接收者的订阅方法永远执行在新的线程中

    Background 如果发布者发布消息是在主线程中,那么订阅者函数在子线程中执行,如果在子线程中发布消息,那么订阅者函数在与发布消息
    相同的线程中执行


    5.事件的优先级

    1.优先级高的先执行
    2.可以通过cancelDelivyEvent来终止事件的传递


    6.Eventbus在Apt注解的预处理框架中使用

    1.在Project 工程目录下的build.gradle文件中添加 apt注解框架的插件包

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:2.2.0'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }

    2.需要在使用Eventbus模块下的buld.gradle文件中添加如下代码

    apply plugin: 'com.neenbedankt.android-apt'
    dependencies {
        compile 'org.greenrobot:eventbus:3.0.0'
        apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
    }
    apt {
        arguments {
           eventBusIndex "com.zoushang.rensheng.dianfeng.SubscrieIndex"
        }
    }

    3.我们需要自定义一个Application
    在Oncreate方法中加入如下代码

    EventBus.builder().addIndex(new SubscrieIndex()).installDefaultEventBus();

  • 相关阅读:
    用spring boot 来创建第一个application
    Entily实体类
    ORM
    lambda expression
    Domain logic approochs
    mysql的数据类型(Data type)
    Backup &recovery备份和还原
    spring AOP Capability and goals
    CDI Features
    Tomcat的配置与安装
  • 原文地址:https://www.cnblogs.com/uf0-java/p/6186672.html
Copyright © 2011-2022 走看看