一、概述
EventBus是一款针对Android优化的公布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service。线程之间传递消息.长处是开销小。代码更优雅。以及将发送者和接收者解耦。
比方假设多层的Fragment之间的通讯,通讯起来就是比較麻烦的,假设重复使用 自己定义广播的话就会造成软件性能的下降。EventBus的出现便非常好的解决的这个问题,使用它你能够非常方便的让各组件之间进行通讯,而不会占用太多的内存。仅仅须要几行代码便可轻松解决。
1、下载EventBus的类库
源代码:https://github.com/greenrobot/EventBus
eclipse 使用源代码或者jar包
android studio 毫不犹豫的Gradle吧
2、基本使用
(1)自己定义一个类,能够是空类。比方:
publicclassAnyEventType {
publicAnyEventType(){}
}
(2)在要接收消息的页面注冊:
eventBus.register(this);
(3)发送消息
eventBus.post(newAnyEventType event);
(4)接受消息的页面实现(共同拥有四个函数,各功能不同,这是当中之中的一个,能够选择性的实现,这里先实现一个):
publicvoidonEvent(AnyEventType event) {}
(5)解除注冊
eventBus.unregister(this);
顺序就是这么个顺序。可真正让自己写。预计还是云里雾里的,以下举个样例来说明下。
首先。在EventBus中,获取实例的方法通常是採用EventBus.getInstance()来获取默认的EventBus实例,当然你也能够new一个又一个。个人感觉还是用默认的比較好。以防出其它问题
Demo
首先描写叙述下Demo。在第一个界面有一个button,点击进入第二个界面,第二个界面相同有一个button,点击第二界面的button把须要发送的信息发送到第一个界面(在这里不是使用intent传值,intent能够不转跳),在第一个界面处理传来的信息。
布局很easy不再贴出。
MAinACtivity
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注冊
}//处理第二个界面传来的事件
public void onEventMainThread(StringEvent event) {
String msg=event.getMsg();
Toast.makeText(MainActivity.this, msg+"onEventMainThread", Toast.LENGTH_LONG).show();
Log.i("onEventMainThread", msg);
}
}
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) {
//发送事件
EventBus.getDefault().post(new StringEvent("我是来自SecondACtivity的參数"));
}
}
自己定义的事件订阅类(依据须要自己定义就可以)
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进行进一步的研究