eventBus:通过解耦发布者和订阅者简化android事件的传递(一个android事件发布/订阅轻量级框架)
优点:代码简洁,是一种发布订阅设计模式(观察者模式)
使用场景:用于线程之间通讯代替Handler,或用于组件之间的通讯代替intent
基本使用:
1、基本框架:两个activity之间的跳转
2、在( 第一个activity) onCreate中注册,在onDestroy中解注册(反注册)具体用法如下:
- package com.mytest;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- 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;
- /**
- * eventBus 在onCreate里面注册
- * 在OnDestroy里面反注册
- */
- public class MainActivity extends AppCompatActivity {
- private Button btn_update;
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn_update = findViewById(R.id.update);
- tv = findViewById(R.id.tv_upData);
- <span style="color:#ff0000;">// 注册eventBus
- EventBus.getDefault().register(this);</span>
- btn_update.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent =new Intent(getApplicationContext(),EVentBusActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- <span style="color:#ff0000;"> EventBus.getDefault().unregister(this);//反注册</span>
- }
- <span style="color:#ff0000;"> /**
- * 定义方法时,要在该方法上添加@Subscribe 否则方法不被执行
- * @param event
- */</span>
- <span style="color:#3333ff;"> @Subscribe()</span>
- public void onEventMainThread(FirstEvent event){
- String msg="onEvent收到消息"+event.getMsg();
- Log.e("harv",msg );
- tv.setText(msg);
- Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
- }
- }
3、在第二个activity中发送消息(eventBusActivity)具体用法如下:
- package com.mytest;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import org.greenrobot.eventbus.EventBus;
- public class EVentBusActivity extends AppCompatActivity {
- private Button btn_event_bus;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_event_bus);
- initView();
- }
- private void initView() {
- btn_event_bus = findViewById(R.id.btn_event_bus);
- btn_event_bus.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // 发送消息是使用EventBus中的Post方法来实现发送的
- EventBus.getDefault().post(new FirstEvent(" event_bus 被点击了"));
- }
- });
- }
- }
4、其中在两个activity中都用到了一个FirstEvent的类。用于获取getmsg(),代码如下:
- package com.mytest;
- /**
- * Created by Administrator on 2018/5/9/009.
- */
- public class FirstEvent {
- String msg;
- public FirstEvent(String msg) {
- this.msg = msg;
- }
- public String getMsg() {
- return msg;
- }
- }
- <span style="color:rgb(255,0,0);"> * 定义方法时,要在该方法上添加@Subscribe 否则方法不被执行
- * @param event
- */</span>
参考文献:http://blog.youkuaiyun.com/harvic880925/article/details/40660137
-------------------------------------------------------------------------------------------------------------------
二、eventBus有四个方法
1、onEventMainThread();
2、onEventBackgroundThread();
3、onEventAsync();
4、onEvent();
onEvent:该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在此方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件会在UI线程中运行,这个在Android中是非常有用的,因为在Android中UI只能在线程中更新,所以在该方法中是不能执行耗时操作的。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
源码如下:
- package com.mytest;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- 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;
- /**
- * eventBus 在onCreate里面注册
- * 在OnDestroy里面反注册
- */
- public class MainActivity extends AppCompatActivity {
- private Button btn_update;
- private TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- btn_update = findViewById(R.id.update);
- tv = findViewById(R.id.tv_upData);
- // 注册eventBus
- EventBus.getDefault().register(this);
- btn_update.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent =new Intent(getApplicationContext(),EVentBusActivity.class);
- startActivity(intent);
- }
- });
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- EventBus.getDefault().unregister(this);
- }
- /**
- * 定义方法时,要在该方法上添加@Subscribe 否则方法不被执行
- * 加注解提示:在写接收消息的方法是,方法名可以自定义,权限必须是public,
- 参数必须是一个只要接收的参数类型是一致的,那么四个方法都可以接收到发送的信息 注释:给人看 注解:给机器看
- ThreadMode.MAIN表示这个方法在主线程中执行(适合做异步加载,可以将子线程加载到数据直接设置到UI界面里
- * @param event
- */
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onEventMainThread(FirstEvent event){
- String msg="onEvent收到消息"+event.getMsg();
- Log.e("harv",msg );
- tv.setText(msg);
- Log.d("firstEvent","onEventMainThread接收到的消息:"+event.getMsg());
- Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
- }
- //SecondEvent接收到的函数一
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onEventMainThread(SecondEvent event){
- Log.d("secEvent","onEventMainThread接收到的消息:"+event.getMsg());
- }
- //SecondEvent接收到的函数二
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onEventBackgroundThread(SecondEvent event){
- Log.d("secEvent","onEventBackgroundThread接收到的消息:"+event.getMsg());
- }
- //SecondEvent接收到的函数三
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onEventAsync(SecondEvent event){
- Log.d("secEvent","onEventAsync接收到的消息:"+event.getMsg());
- }
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onEvent(ThirdEvent event){
- Log.d("thirdEvent","onEvent接收到的消息:"+event.getMsg());
- }
- }
运行结果:log日志截图:
参考文献:http://blog.youkuaiyun.com/harvic880925/article/details/40787203
源码下载:https://download.youkuaiyun.com/download/qq_36636969/10402716