Android EventBus的基本使用
2016-04-04 13:23
573 查看
怕自己以后忘了怎么用。。。
EventBus is a publish/subscribe event bus optimized for Android.
EventBus...
simplifies the communication between components
decouples event senders and receivers
performs well with Activities, Fragments, and background threads
avoids complex and error-prone dependencies and life cycle issues
makes your code simpler
is fast
is tiny (~50k jar)
is proven in practice by apps with 100,000,000+ installs
has advanced features like delivery threads, subscriber priorities, etc.
其实简单的说就是简化了各个模块见得通信,
优点1.解耦了发送者接收者
2.在Activity,fragment,后台线程运行良好
3.避免在处理生命周期中容易出现的一些问题
//事件二
//事件三
//MainActivity.java
注意:这里接收消息,但是同样需要,注册和解注册EventBus.getDefault().register(this)和EventBus.getDefault().unregister(this),否则接收不到消息
//SecondActivity.java
(图片待添加)
可以看出
点击eventone
xcqw onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked
xcqw Second onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked(这个是写在SecondActivity,也能接收到消息)
点击eventtwo
xcqw onEvent收到了消息:MainEventTwo:EventTwo btn clicked
xcqw onEventMainThread收到了消息:MainEventTwo:EventTwo btn clicked
点击eventthree
xcqw onEventMainThread收到了消息:MainEventEventThree:EventThree btn clicked
A:不管是接收还是发送的activity和fragment都需要注册和解注册EventBus.getDefault().register(this)和EventBus.getDefault().unregister(this)
Q2:靠的是什么接收,也就是为什么这个函数能接收,那么函数不行?
A:根据发送的参数和接收函数的参数是否一样,只要接收函数一样,全都能接收到,可以从点击EventOne中可以看出来,MainActivity和SecondActivity中
//发送者
//MainActivity.java
//SecondActivity.java
Q3:如果有多个接收同一时间的函数,会不会都执行?
A:从上一个问题就可以看出结果,有多少个都会执行
Q4:有四个函数分别代表什么意思
A:
EventBus包含4个ThreadMode:PostThread,MainThread,BackgroundThread,Async
MainThread我们已经不陌生了;我们已经使用过。
具体的用法,极其简单,方法名为:onEventPostThread, onEventMainThread,onEventBackgroundThread,onEventAsync即可
具体什么区别呢?
onEvent来接收事件,那么接收事件和分发事件在一个线程中执行
onEventMainThread代表这个方法会在UI线程执行
onEventPostThread代表这个方法会在当前发布事件的线程执行
BackgroundThread这个方法,如果在非UI线程发布的事件,则直接执行,和发布在同一个线程中。如果在UI线程发布的事件,则加入后台任务队列,使用线程池一个接一个调用。
Async 加入后台任务队列,使用线程池调用,注意没有BackgroundThread中的一个接一个。
一.背景(直接从github直接抄过来)
github地址:https://github.com/greenrobot/EventBusEventBus is a publish/subscribe event bus optimized for Android.
EventBus...
simplifies the communication between components
decouples event senders and receivers
performs well with Activities, Fragments, and background threads
avoids complex and error-prone dependencies and life cycle issues
makes your code simpler
is fast
is tiny (~50k jar)
is proven in practice by apps with 100,000,000+ installs
has advanced features like delivery threads, subscriber priorities, etc.
其实简单的说就是简化了各个模块见得通信,
优点1.解耦了发送者接收者
2.在Activity,fragment,后台线程运行良好
3.避免在处理生命周期中容易出现的一些问题
二.代码
//事件1/** * Created by Administrator on 2016/3/29 0029. */ public class EventOne { private String mMsg; public EventOne(String msg) { // TODO Auto-generated constructor stub mMsg = "MainEventEventOne:"+msg; } public String getMsg(){ return mMsg; } }
//事件二
/** * Created by Administrator on 2016/3/29 0029. */ public class EventTwo { private String mMsg; public EventTwo(String msg) { // TODO Auto-generated constructor stub mMsg = "MainEventTwo:" + msg; } public String getMsg() { return mMsg; } }
//事件三
/** * Created by Administrator on 2016/3/29 0029. */ public class EventThree { private String mMsg; public EventThree(String msg) { // TODO Auto-generated constructor stub mMsg = "MainEventEventThree:" + msg; } public String getMsg() { return mMsg; } }
//MainActivity.java
public class MainActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.bt_jump).setOnClickListener(this); EventBus.getDefault().register(this); } public void onEventMainThread(EventOne event) { System.out.println("xcqw onEventMainThread收到了消息:" + event.getMsg()); } public void onEventMainThread(EventTwo event) { System.out.println("xcqw onEventMainThread收到了消息:" + event.getMsg()); } public void onEventMainThread(EventThree event) { System.out.println("xcqw onEventMainThread收到了消息:" + event.getMsg()); } public void onEvent(EventTwo event) { System.out.println("xcqw onEvent收到了消息:" + event.getMsg()); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Override public void onClick(View v) { startActivity(new Intent(this, SecondActivity.class)); } }
注意:这里接收消息,但是同样需要,注册和解注册EventBus.getDefault().register(this)和EventBus.getDefault().unregister(this),否则接收不到消息
//SecondActivity.java
public class SecondActivity extends Activity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); findViewById(R.id.bt_one).setOnClickListener(this); findViewById(R.id.bt_two).setOnClickListener(this); findViewById(R.id.bt_three).setOnClickListener(this); //eventbus注册 EventBus.getDefault().register(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_one: EventBus.getDefault().post( new EventOne("EventOne btn clicked")); break; case R.id.bt_two: EventBus.getDefault().post( new EventTwo("EventTwo btn clicked")); break; case R.id.bt_three: EventBus.getDefault().post( new EventThree("EventThree btn clicked")); break; } } public void onEventMainThread(EventOne item) { System.out.println("xcqw Second onEventMainThread收到了消息:" + item.getMsg()); } @Override protected void onDestroy() { super.onDestroy(); //eventbus解注册 EventBus.getDefault().unregister(this); } }
(图片待添加)
可以看出
点击eventone
xcqw onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked
xcqw Second onEventMainThread收到了消息:MainEventEventOne:EventOne btn clicked(这个是写在SecondActivity,也能接收到消息)
点击eventtwo
xcqw onEvent收到了消息:MainEventTwo:EventTwo btn clicked
xcqw onEventMainThread收到了消息:MainEventTwo:EventTwo btn clicked
点击eventthree
xcqw onEventMainThread收到了消息:MainEventEventThree:EventThree btn clicked
(1)现在说说几个常见问题
Q1:什么时候注册,什么时候不注册A:不管是接收还是发送的activity和fragment都需要注册和解注册EventBus.getDefault().register(this)和EventBus.getDefault().unregister(this)
Q2:靠的是什么接收,也就是为什么这个函数能接收,那么函数不行?
A:根据发送的参数和接收函数的参数是否一样,只要接收函数一样,全都能接收到,可以从点击EventOne中可以看出来,MainActivity和SecondActivity中
//发送者
EventBus.getDefault().post( new EventOne("EventOne btn clicked"));
//MainActivity.java
public void onEventMainThread(EventOne event) { System.out.println("xcqw onEventMainThread收到了消息:" + event.getMsg()); }
//SecondActivity.java
public void onEventMainThread(EventOne item) { System.out.println("xcqw Second onEventMainThread收到了消息:" + item.getMsg()); }
Q3:如果有多个接收同一时间的函数,会不会都执行?
A:从上一个问题就可以看出结果,有多少个都会执行
Q4:有四个函数分别代表什么意思
A:
EventBus包含4个ThreadMode:PostThread,MainThread,BackgroundThread,Async
MainThread我们已经不陌生了;我们已经使用过。
具体的用法,极其简单,方法名为:onEventPostThread, onEventMainThread,onEventBackgroundThread,onEventAsync即可
具体什么区别呢?
onEvent来接收事件,那么接收事件和分发事件在一个线程中执行
onEventMainThread代表这个方法会在UI线程执行
onEventPostThread代表这个方法会在当前发布事件的线程执行
BackgroundThread这个方法,如果在非UI线程发布的事件,则直接执行,和发布在同一个线程中。如果在UI线程发布的事件,则加入后台任务队列,使用线程池一个接一个调用。
Async 加入后台任务队列,使用线程池调用,注意没有BackgroundThread中的一个接一个。
相关文章推荐
- Android应用自定义View绘制方法手册
- Android通过来电号码识别姓名。
- Android焦点分发基本流程
- 萤石Android SDK 集成到AndroidStudio的时候报错,Tag <activity> attribute name has invalid character ' '.
- Android学习之界面篇(三)LayoutAnimationController详细介绍
- Android 学习记录-ImageView显示格式
- Android焦点分发基础
- Android中使用PreferenceActivity创建菜单
- Android属性动画
- 遇到问题----Android----ActivityManager: Error: Activity class {xx/xx.MainActivity} does not exist
- 遇到问题----Android----ActivityManager: Error: Activity class {xx/xx.MainActivity} does not exist
- 在一个应用中打开另一个应用
- Android开发实用技巧之四:自定义ActivityCollector来管理活动
- Android WebView的Js对象注入漏洞解决方案
- Android中的万能适配器——base-adapter-helper解析
- Android:自定义标题栏
- High Performance之android高性能之路
- Android 属性动画(Property Animation) 完全解析 (下)
- [android] notification入门
- #号称5秒1000个包的超快速多渠道打包方式