Android EventBus
2016-01-29 11:40
148 查看
在编程过程中,当我们想通知其他组件某些事情发生时,我们通常使用观察者模式,正式因为观察者模式非常常见,所以在jdk1.5中已经帮助我们实现了观察者模式,我们只需要简单的继承一些类就可以快速使用观察者模式,在Android中也有一个类似功能的开源库EventBus,可以很方便的帮助我们实现观察者模式,那么我们就开始学习如何使用EventBus.
在接下来的内容中,我首先会介绍如何使用EventBus,然后再简单的学习一下EventBus的底层实现原理,因为仅仅学习如何使用总是感觉内心不够踏实,万一哪天出了Bug也无从下手,了解了它的基本实现后,就会用得游刃有余。好了 废话不多说,下面开始学习吧
1、下载EventBus库:
EvnetBus的下载地址:https://github.com/greenrobot/EventBus.git
2、将EventBus.jar放入自己工程的libs目录即可
3、定义一个事件,这个事件一旦被EventBus分发出去就是代表某一件事情发生了,这个事件就是某个观察者关心的事情(不需要继承任何类)
4、定义观察者,然后将该观察者注册到EventBus
5、由EventBus分发事件,告知观察者某一件事情发生了
6、使用完成后从EventBus中反注册观察者。
熟悉观察者模式的朋友肯定对于上面的流程非常熟悉,其实和观察模式基本是一样的。但是也是有区别的。在观察者模式中,所有的观察者都需要实现一个接口,这个接口有一个统一的方法如:
public void onUpdate();
然后当某一个事件发生时,某个对象会调用观察者的onUpdate方法通知观察者某件事情发生了,但是在EventBus中不需要这样,EventBus中是这样实现的:
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
上面列出的这四个函数,关键问题在于,我们怎么指定调用哪个函数呢?
我们先研究一下,上一篇中是怎么调用的onEventMainThread函数,除了在接收端注册与反注册以后,关键问题在于新建的一个类:
新建一个类:
[java] view
plain copy
package com.harvic.other;
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
// TODO Auto-generated constructor stub
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
发送时:
[java] view
plain copy
EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
接收时:
[java] view
plain copy
public void onEventMainThread(FirstEvent event) {
……
}
发送时发送的是这个类的实例,接收时参数就是这个类实例。
[java] view
plain copy
package com.harvic.other;
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
// TODO Auto-generated constructor stub
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
所以!!!!!!当发过来一个消息的时候,EventBus怎么知道要调哪个函数呢,就看哪个函数传进去的参数是这个类的实例,哪个是就调哪个。那如果有两个是呢,那两个都会被调用!!!!
然后在SecondActivity中新建三个按钮,分别发送不同的类的实例,代码如下:
[java] view
plain copy
package com.harvic.tryeventbus2;
import com.harvic.other.FirstEvent;
import com.harvic.other.SecondEvent;
import com.harvic.other.ThirdEvent;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
btn_SecondEvent = (Button) findViewById(R.id.btn_second_event);
btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event);
btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new FirstEvent("FirstEvent btn clicked"));
}
});
btn_SecondEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new SecondEvent("SecondEvent btn clicked"));
}
});
btn_ThirdEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new ThirdEvent("ThirdEvent btn clicked"));
}
});
}
}
在MainActivity中,除了注册与注册,我们利用onEventMainThread(FirstEvent event)来接收来自FirstEvent的消息,使用onEventMainThread(SecondEvent event)接收来自SecondEvent 实例的消息,使用onEvent(ThirdEvent event) 来接收ThirdEvent
实例的消息。
[java] view
plain copy
package com.harvic.tryeventbus2;
import com.harvic.other.FirstEvent;
import com.harvic.other.SecondEvent;
import com.harvic.other.ThirdEvent;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
TextView tv;
EventBus eventBus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
btn = (Button) findViewById(R.id.btn_try);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
}
});
}
public void onEventMainThread(FirstEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEventMainThread(SecondEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEvent(ThirdEvent event) {
Log.d("harvic", "OnEvent收到了消息:" + event.getMsg());
}
public void onEventBackgroundThread(SecondEvent event) {
Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg());
}
public void onEventAsync(SecondEvent event) {
Log.d("harvic", "onEventAsync收到了消息:" + event.getMsg());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
发现什么问题了没?
在接下来的内容中,我首先会介绍如何使用EventBus,然后再简单的学习一下EventBus的底层实现原理,因为仅仅学习如何使用总是感觉内心不够踏实,万一哪天出了Bug也无从下手,了解了它的基本实现后,就会用得游刃有余。好了 废话不多说,下面开始学习吧
1、下载EventBus库:
EvnetBus的下载地址:https://github.com/greenrobot/EventBus.git
2、将EventBus.jar放入自己工程的libs目录即可
3、定义一个事件,这个事件一旦被EventBus分发出去就是代表某一件事情发生了,这个事件就是某个观察者关心的事情(不需要继承任何类)
4、定义观察者,然后将该观察者注册到EventBus
5、由EventBus分发事件,告知观察者某一件事情发生了
6、使用完成后从EventBus中反注册观察者。
熟悉观察者模式的朋友肯定对于上面的流程非常熟悉,其实和观察模式基本是一样的。但是也是有区别的。在观察者模式中,所有的观察者都需要实现一个接口,这个接口有一个统一的方法如:
public void onUpdate();
然后当某一个事件发生时,某个对象会调用观察者的onUpdate方法通知观察者某件事情发生了,但是在EventBus中不需要这样,EventBus中是这样实现的:
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
二、实战
1、解析
上面列出的这四个函数,关键问题在于,我们怎么指定调用哪个函数呢?我们先研究一下,上一篇中是怎么调用的onEventMainThread函数,除了在接收端注册与反注册以后,关键问题在于新建的一个类:
新建一个类:
[java] view
plain copy
package com.harvic.other;
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
// TODO Auto-generated constructor stub
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
发送时:
[java] view
plain copy
EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
接收时:
[java] view
plain copy
public void onEventMainThread(FirstEvent event) {
……
}
发送时发送的是这个类的实例,接收时参数就是这个类实例。
[java] view
plain copy
package com.harvic.other;
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
// TODO Auto-generated constructor stub
mMsg = msg;
}
public String getMsg(){
return mMsg;
}
}
所以!!!!!!当发过来一个消息的时候,EventBus怎么知道要调哪个函数呢,就看哪个函数传进去的参数是这个类的实例,哪个是就调哪个。那如果有两个是呢,那两个都会被调用!!!!
2、发送
然后在SecondActivity中新建三个按钮,分别发送不同的类的实例,代码如下:[java] view
plain copy
package com.harvic.tryeventbus2;
import com.harvic.other.FirstEvent;
import com.harvic.other.SecondEvent;
import com.harvic.other.ThirdEvent;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SecondActivity extends Activity {
private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
btn_SecondEvent = (Button) findViewById(R.id.btn_second_event);
btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event);
btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new FirstEvent("FirstEvent btn clicked"));
}
});
btn_SecondEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new SecondEvent("SecondEvent btn clicked"));
}
});
btn_ThirdEvent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
EventBus.getDefault().post(
new ThirdEvent("ThirdEvent btn clicked"));
}
});
}
}
3、接收
在MainActivity中,除了注册与注册,我们利用onEventMainThread(FirstEvent event)来接收来自FirstEvent的消息,使用onEventMainThread(SecondEvent event)接收来自SecondEvent 实例的消息,使用onEvent(ThirdEvent event) 来接收ThirdEvent实例的消息。
[java] view
plain copy
package com.harvic.tryeventbus2;
import com.harvic.other.FirstEvent;
import com.harvic.other.SecondEvent;
import com.harvic.other.ThirdEvent;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button btn;
TextView tv;
EventBus eventBus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
btn = (Button) findViewById(R.id.btn_try);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
}
});
}
public void onEventMainThread(FirstEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEventMainThread(SecondEvent event) {
Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg());
}
public void onEvent(ThirdEvent event) {
Log.d("harvic", "OnEvent收到了消息:" + event.getMsg());
}
public void onEventBackgroundThread(SecondEvent event) {
Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg());
}
public void onEventAsync(SecondEvent event) {
Log.d("harvic", "onEventAsync收到了消息:" + event.getMsg());
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
发现什么问题了没?
相关文章推荐
- android studio获取sha1值(windows平台下)
- Android中使用Service实现后台发送邮件功能实例
- Android imageView1.基础属性
- Android apk 加密方式
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- Android 基础之 TextView运用
- android 系统主界面底部工具栏上的图标显示与修改
- Android之传感器(二)加速度传感器
- Android监听手机电话状态与发送邮件通知来电号码的方法(基于PhoneStateListene实现)
- android 怎么判断自定义的控件的显示状态(隐藏、显示)
- ubuntu 上安装kivy
- AndroidStudio中使用开源框架android-gif-drawable
- Android之传感器(一)光照传感器
- android项目中出现的错误
- 详解Android自定义控件属性TypedArray以及attrs
- android实现仿QQ登陆界面的多账号保存
- 4000 android 有确认和取消按钮的 AlertDialog 弹出的使用说明
- Android Studio导入Fresco
- Android实现计时与倒计时(限时抢购)的几种方法
- Android开发常用的adb命令整理