Android的EventBus使用

Android的EventBus使用

简介
EventBus简介

使用
添加到工程、使用
EventBus使用详解

这个EventBus不要太好用啊,简直是开发者福音。上手极快,只需导入,即刻使用!!!!
解耦效果贼棒!!
在一个Activity、Fragment中post(),即可在另一个Activity、Fragment中subscribe()
想要传递什么样的值完全取决于你的实体类定义成啥样,传入啥类就接收啥类实体,简直不要太便捷!!!!

代码示例

比如我先这样:

import android.bluetooth.BluetoothDevice;
/**
 * 新构建一个类,用于数据的载体.
 */
public class ReturnBluetoothDevice {
    private BluetoothDevice device;
    public ReturnBluetoothDevice(BluetoothDevice device){
        this.device=device;
    }
    public BluetoothDevice getDeivce(){
        return device;
    }
    public void setDevice(BluetoothDevice device){
        this.device=device;
    }
}

然后再这样:

//调用post,发送一个类实体
EventBus.getDefault().post(new ReturnBluetoothDevice(device));

最后这样:

/*
 * 这里定义一个接收的方法就行,方法名无所谓
 * 前面必须要加@Subscribe,表明身份,GKD!!!
 * @param device
*/
@Subscribe
public void onEventThread(ReturnBluetoothDevice device){
//这里加入接收到EventBus消息需要进行的处理,不能做耗时处理
	Bluetooth mDevice;
    mDevice = device.getDevice();
}

然后就ok了,是不是很简单!!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值