Android 事件总线OTTO使用说明和源码解析

一、Otto简单介绍

OTTO是Square推出的库,地址:https://github.com/square/otto

先来看看otto的官方介绍

An enhanced Guava-based event bus with emphasis on Android support.Otto is an event bus designed to decouple different parts of your application while still allowing them to communicate efficiently.Forked from Guava, Otto adds unique functionality to an already refined event bus as well as specializing it to the Android platform.

OTTO基于Guava项目的Android支持库,如果你在Android程序开发的过程中想要不同的组件之间进行有效的通信可以使用这个库。通过otto库可以。

二、Otto简单使用

1、创建一个BUS的单例。

public class AppConfig {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
    return BUS;
}
}

2、在需要使用Otto的类中注册

在类创建好之后,或者需要重新注册的时候注册,一般在Activity的onCreate()或者onPause()方法中

 AppConfig.getBusInstance().register(this);

3、定义订阅方法

@Subscribe
public void onWallpaperUpdate(MyObject obj) {
    //对obj进行需要的逻辑处理
}

4、发送消息

AppConfig.getBusInstance().post(myobj);

5、解绑

注意在类销毁的时候或者暂时不需要再收消息的时候解绑,,一般在Activity的onDestroy()或者onResume()方法中

 AppConfig.getBusInstance().unregister(this);

三、Otto源码解析

1、整体结构

otto的源码结构非常简单,所有类都包含
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值