一、概述
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
比如如果多层的Fragment之间的通讯,通讯起来就是比较麻烦的,如果反复使用 自定义广播的话就会造成软件性能的下降,EventBus的出现便很好的解决的这个问题,使用它你可以很方便的让各组件之间进行通讯,而不会占用太多的内存,只需要几行代码便可轻松解决。
1、下载EventBus的类库
源码:https://github.com/greenrobot/EventBus
eclipse 使用源码或者jar包
android studio 毫不犹豫的Gradle吧
2、基本使用
(1)自定义一个类,可以是空类,比如:
- public class AnyEventType {
- public AnyEventType(){}
- }
(2)在要接收消息的页面注册:
- eventBus.register(this);
(3)发送消息
- eventBus.post(new AnyEventType event);
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
- public void onEvent(AnyEventType event) {}
- eventBus.unregister(this);
首先,在EventBus中,获取实例的方法一般是采用EventBus.getInstance()来获取默认的EventBus实例,当然你也可以new一个又一个,个人感觉还是用默认的比较好,以防出其他问题
Demo
首先描述下Demo,在第一个界面有一个按钮,点击进入第二个界面,第二个界面同样有一个按钮,点击第二界面的按钮把需要发送的信息发送到第一个界面(在这里不是使用intent传值,intent可以不转跳),在第一个界面处理传来的信息。
布局非常简单不再贴出。
MAinACtivity
<span style="color:#333333;">package com.example.zhuoyou.eventbus;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.zhuoyou.eventbus.com.example.zhuoyou.eventbus.domain.StringEvent;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this);
button = (Button) findViewById(R.id.go);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();//取消EventBus注册
}//处理第二个界面传来的事件
</span><span style="color:#ff6666;"> public void onEventMainThread(StringEvent event) {
String msg=event.getMsg();
Toast.makeText(MainActivity.this, msg+"onEventMainThread", Toast.LENGTH_LONG).show();
Log.i("onEventMainThread", msg);
}</span><span style="color:#333333;">
}
</span>
SecondActivity
package com.example.zhuoyou.eventbus;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.zhuoyou.eventbus.com.example.zhuoyou.eventbus.domain.StringEvent;
import java.net.HttpURLConnection;
import de.greenrobot.event.EventBus;
public class SecondActivity extends Activity implements View.OnClickListener {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button= (Button) findViewById(R.id.Return);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
<span style="white-space:pre"> </span>//发送事件
EventBus.getDefault().post(new StringEvent("我是来自SecondACtivity的参数"));
}
}<strong>
</strong>
自定义的事件订阅类(根据需要自定义即可)
package com.example.zhuoyou.eventbus.com.example.zhuoyou.eventbus.domain;
/**
* 项目名称:My Application
* 包名:com.example.zhuoyou.eventbus.com.example.zhuoyou.eventbus.domain
* 作者: flyou
* 创建时间:15/7/24 14:08
* 描述:事件订阅类
*/
public class StringEvent {
private String Msg;
public String getMsg() {
return Msg;
}
public void setMsg(String msg) {
Msg = msg;
}
public StringEvent(String msg) {
Msg = msg;
}
}
在代码中,需要在oncreat中进行注册,然后在onDestory方法中取消监听,使用 onEventMainThread处理传来的事件。
今天先简单介绍下EventBus的使用,下次回具体对EventBus进行进一步的研究