EventBus3.0.0如何在Android应用中使用-创新互联

本篇文章给大家分享的是有关EventBus 3.0.0如何在Android 应用中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

成都创新互联从2013年创立,先为姚安等服务建站,姚安等地企业,进行企业商务咨询服务。为姚安企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

EventBus框架

EventBus是一个通用的叫法,例如Google出品的Guava,Guava是一个庞大的库,EventBus只是它附带的一个小功能,因此实际项目中使用并不多。用的最多的是greenrobot/EventBus,这个库的优点是接口简洁,集成方便,但是限定了方法名,不支持注解。另一个库square/otto修改自 Guava ,用的人也不少。所以今天我们研究的目标是greenrobot的EventBus.

EventBus 简介

1、EventBus3.0.0 是最新的版本。
2、EventBus 是Android 发布/订阅事件总线,可简化 Activities, Fragments, Threads, Services 等组件间的消息传递。
3、可替代 Intent, Handler, BroadCast ,接口等传统方案,更快,代码更小,50K 左右的 jar 包,代码更优雅,彻底解耦。

github地址:https://github.com/greenrobot/EventBus

EventBus原理图

EventBus 3.0.0如何在Android 应用中使用

如何添加依赖

在module的build.gredle 文件中的dependencies标签中添加

compile 'org.greenrobot:eventbus:3.0.0'

例如

apply plugin: 'com.android.application'

android {
  compileSdkVersion 24
  buildToolsVersion "24.0.3"

  defaultConfig {
    applicationId "com.eventbus.app"
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"

  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile 'com.android.support:appcompat-v7:24.2.1'

  compile 'org.greenrobot:eventbus:3.0.0'
}

网页标题:EventBus3.0.0如何在Android应用中使用-创新互联
标题网址:http://ybzwz.com/article/hisjc.html