EventBus(https://github.com/greenrobot/EventBus)
EventBus is a publish/subscribe event bus optimized for Android.(EventBus是Android的发布/订阅事件总线优化。)
1.simplifies the communication between components(简化了组件之间的通信)
(1)decouples event senders and receivers(将事件发送者和接受者解耦)
(2)performs well with Activities, Fragments, and background threads(很好的执行活动、碎片和后台线程)
(3)avoids complex and error-prone dependencies and life cycle issues(避免了复杂的和容易出错的依赖性和生命周期问题)
2.makes your code simpler(让你的代码变得更简洁)
3.is fast(很快)
4.is tiny (~50k jar)(很小(jar包只有50k))
5.is proven in practice by apps with 100,000,000+ installs(在实践中超过100,000,000的app安装得到了证明)
6.has advanced features like delivery threads, subscriber priorities, etc.(拥有先进的功能,如交付线程,用户优先级等。)
EventBus in 3 steps(EventBus使用的3个步骤):
1.Define events:(定义事件)
public static class MessageEvent {
/* Additional fields if needed */ (如果需要额外的字段)
}
2.Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:(准备订阅者:声明和注释你的订阅方法,选择指定线程模式)
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:(注册和注销你的用户。例如在Android上,活动和碎片应该根据其生命周期注册)
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
(官网上面写了两个Activity生命周期中注册和注销,但我的建议是在Oncreate方法中注册,OnDestroy方法中注销)
3.Post events(发布事件)
EventBus.getDefault().post(new MessageEvent());
依赖:
Gradle:
compile 'org.greenrobot:eventbus:3.0.0'
package xuechuan.com.myeventbustest; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView mTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); mTv = (TextView) findViewById(R.id.tv); Button button = (Button) findViewById(R.id.btn_main); button.setOnClickListener(this); } @Override public void onClick(View view) { int id = view.getId(); switch (id) { case R.id.btn_main: Intent intent = new Intent(MainActivity.this,SecondActivity.class); startActivity(intent); } } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMessage(EventMessage event) { String msg = event.getMsg(); mTv.setText(msg); Toast.makeText(this,msg,Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); //反注册EventBus EventBus.getDefault().unregister(this); } }
package xuechuan.com.myeventbustest; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sencond); Button btnSendEvent = (Button) findViewById(R.id.btn_second); btnSendEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //发送消息 EventBus.getDefault().post(new EventMessage("我这边好了,你可以修改UI了")); } }); } }
package xuechuan.com.myeventbustest; /** * Created by Administrator on 2017/3/26. */ public class EventMessage { private String mMsg; public EventMessage(String msg) { this.mMsg=msg; } public String getMsg() { return mMsg; } }