-
由greenrobot组织贡献(该组织还贡献了greenDAO),一个Android事件发布/订阅轻量级框架,
功能:通过解耦发布者和订阅者简化Android事件传递
EventBus可以代替Android传统的Intent,Handler,Broadcast或接口函数,在Fragment,Activity,Service线程之间传递数据,执行方法。
特点:代码简洁,是一种发布订阅设计模式(观察者设计模式)。
参考了大牛的博客:
刘望舒:
Android事件总线(一)EventBus3.0用法全解析
启舰:
EventBus使用详解(一)——初步使用EventBus
总李写代码
Android消息传递之EventBus 3.0使用详解
1.添加依赖
Android Studio 配置的module的gradle内:
compile 'org.greenrobot:eventbus:3.0.0'
2.定义一个消息事件,
public class User {
private String name;
private String pass;
public User(String name, String pass) {
this.name = name;
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", pass='" + pass + '\'' +
'}';
}
}
一,普通事件(a--->b---值-->a),相当于数据回传
具体的实践:
1.注册和取消订阅事件
PuTong1Activity 中注册和取消订阅事件(PuTong1Activity .java):
public class PuTong1Activity extends AppCompatActivity implements View.OnClickListener {
private Button putong1_btn;
private TextView putong1Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_tong1);
initView();
//注册事件
EventBus.getDefault().register(this);
}
private void initView() {
putong1_btn = (Button) findViewById(R.id.putong1_btn);
putong1Text = (TextView) findViewById(R.id.putong1Text);
putong1_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
startActivity(new Intent(PuTong1Activity.this,PuTong2Activity.class));
}
//事件订阅者处理事件
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMoonEvent(User user){
putong1Text.setText(user.getName()+"---接收到的值----"+user.getPass());
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册事件
EventBus.getDefault().unregister(this);
}
}
2.事件发布者发布事件PuTong2Activity 类
public class PuTong2Activity extends AppCompatActivity implements View.OnClickListener {
private Button putong2_btn;
private EditText nameEdit;
private EditText passEdit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pu_tong2);
initView();
}
private void initView() {
putong2_btn = (Button) findViewById(R.id.putong2_btn);
nameEdit = (EditText) findViewById(R.id.nameEdit);
passEdit = (EditText) findViewById(R.id.passEdit);
putong2_btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String nameStr = nameEdit.getText().toString().trim();
String passStr = passEdit.getText().toString().trim();
//普通时间发送消息给putong1用post方法
EventBus.getDefault().post(new User(nameStr,passStr));
finish();
}
}
二.粘性事件(相当于直接从a—值—>b,与intent跳转传值类似)
具体的实践:
1.发送黏性事件(MainActivity .java)
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText nameEdit;
private EditText passEdit;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
nameEdit = (EditText) findViewById(R.id.name);
passEdit = (EditText) findViewById(R.id.pass);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String nameStr = nameEdit.getText().toString().trim();
String passStr = passEdit.getText().toString().trim();
if(null==nameStr&&nameStr.equals("")||null==passStr&&passStr.equals("")){
Toast.makeText(this,"用户名密码不能为空",Toast.LENGTH_SHORT).show();
}else{
//2.发送消息粘性事件用postSticky
EventBus.getDefault().postSticky(new User(nameStr,passStr));
//跳转
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
startActivity(intent);
}
}
}
2.订阅粘性事件(ResultActivity.java)
public class ResultActivity extends AppCompatActivity implements View.OnClickListener {
private Button getResult;
private TextView text;
private String nameStr;
private String passStr;
//在接收消息的页面 注册EventBus
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
initView();
}
private void initView() {
getResult = (Button) findViewById(R.id.getResult);
text = (TextView) findViewById(R.id.text);
getResult.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//注册EventBus
EventBus.getDefault().register(this);
}
//订阅者处理粘性事件
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void onEventMainThread(User user) {
String msg = "账号:" + user.getName()+"---密码:"+user.getPass();
Log.d("ResultActivity", msg);
text.setText(msg);
Toast.makeText(ResultActivity.this, msg, Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
//取消注册
EventBus.getDefault().unregister(this);
}
}
就到这了.........