EventBus
来源:互联网 发布:淘宝店运营方案 编辑:程序博客网 时间:2024/04/30 15:40
EventBus
什么是EventBus?
由Greenrobot组织贡献的一个Android事件发布/订阅轻量级框架,EventBus可以代替Android传统的Intent,Handler,Broadcast或接口函数,在Fragment,Activity,Service,Thread之间传递数据.
EventBus的原理 :
- EventBus底层采用的是注解和反射的方式来获取订阅方法信息(首先是注解获取,若注解获取不到,再用反射)
- 当前订阅者是添加到Eventbus 总的事件订阅者的subscriptionByEventType集合中
- 订阅者所有订阅的事件类型添加到typeBySubscriber 中,方便解注册时,移除事件
EventBus的优点 :
- 实现了完全的解绑
- 在传值的时候,不需要真正的有对象接收
- 同一个事件可以被多个对象接收,只要他们的参数类型和发送方一致
- 有快又小,代码简洁,是一种发布订阅设计模式(观察者设计模式)
EventBus和广播对比 :
- EventBus使用起来和动态广播很像,需要注册-取消-注销
- EventBus要比广播占用资源少
- EventBus要比广播安全
- EventBus不能跨进程
- EventBus没有有序广播这样的功能
- EventBus可以方便的切换线程
如何使用EventBus ?
概念总是那么不好理解,所以我们用实际例子去诠释它
需求:
- 有两个Activity,其中一个Activity包含一个Button,一个TextView,点击这个Button跳转到另一个Activity.
- 另一个Activity中也有一个Button,点击Button进行事件的发送
- 当第一个Activity接收到消息后显示吐司(将消息显示出来),并且将消息设置给TextView
step1. 导入EventBus依赖
dependencies { compile 'org.greenrobot:eventbus:3.0.0'}
step2. 创建事件类
/** * If the code is not wrong its author is JokerCats. */public class Event { private String msg; public Event(String msg){ this.msg = msg; } public String getMsg() { return msg; }}
step3. 注册EventBus与注销EventBus
EventBus.getDefault().register(this);EventBus.getDefault().unregister(this);
step4. 发送事件
EventBus.getDefault().post(new Event("Event received (づ ̄3 ̄)づ╭❤~"));
step5. 接收事件
@Subscribe public void setTextView(Event event) { String msg = event.getMsg(); receiverTv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); }
Activity :
温馨提示:页面跳转,需要在Manifest文件中进行注册.
“`
public class OneActivity extends AppCompatActivity {
private Button mainBtn;private TextView receiverTv;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 在要接收消息的页面注册EventBus ()中为接收数据的对象 // 必须保证该对象至少有一个方法被@Subscribe所修饰 EventBus.getDefault().register(this); mainBtn = (Button) findViewById(R.id.main_btn); receiverTv = (TextView) findViewById(R.id.main_tv); mainBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(OneActivity.this, TwoActivity.class); startActivity(intent); } });}// 接收EventBus的消息// @Subscribe 不要忘记!@Subscribepublic void setTextView(Event event) { String msg = event.getMsg(); receiverTv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();}@Overrideprotected void onDestroy() { super.onDestroy(); // 注销EventBus EventBus.getDefault().unregister(this);}
}
``` /** * If the code is not wrong its author is JokerCats. */public class TwoActivity extends AppCompatActivity{ private Button secondBtn; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); secondBtn = (Button) findViewById(R.id.second_btn); secondBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 发送事件 且发送过去的是我们创建的事件类的实例 EventBus.getDefault().post(new Event("Event received (づ ̄3 ̄)づ╭❤~")); } }); }}
Event :
public class Event { private String msg; public Event(String msg){ this.msg = msg; } public String getMsg() { return msg; }}
效果图 :
感谢技术支持:http://blog.csdn.net/harvic880925/article/details/40660137
0 0
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- Eventbus
- EventBus
- EventBus
- EventBus
- EventBus
- C++四种类型转换机制
- block块对象
- String类详解
- [kuangbin带你飞]专题一 简单搜索 F
- [构造 哈密顿路径] SRM 704 div1 HamiltonianConstruction
- EventBus
- 神经网络激活函数的介绍
- poj
- PAT 乙级1005
- 路由器逆向分析------在QEMU MIPS虚拟机上运行MIPS程序(ssh方式)
- Hololens中使用holotoolkit自带的按钮
- Java 多线程3:Lock 接口(接口方法分析,ReentrantLock,ReadWriteLock)
- 以太网芯片MAC和PHY
- IntelliJ IDEA 或Android Studio中提示:Project SDK is not defined