greenrobot EventBus开源库-使用

  • 概述

    greenrobot的EventBus是一个非常流行的开源库
    EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
    源码:https://github.com/greenrobot/EventBus

  • 官方:EventBus is a publish/subscribe event bus optimized for Android.
    这里写图片描述

EventBus 主要分四种事件
1、onEvent
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync


  • onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。

  • onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。

  • onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
  • onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.

Demo
EventBus 使用起来很简单,主要4个步骤:注册->发布订阅->接收订阅事件->注销
1;注册
EventBus.getDefault().register(this);
2:发布
EventBus.getDefault().post(new FirstEvent ( “android” ));
3:接收 (四种事件根据需求进行接收)
public void onEventMainThread(FirstEvent event) {}
4: 注销
EventBus.getDefault().unregister(this);

具体代码:

public class MainActivity extends Activity {

    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EventBus.getDefault().register(this);

        btn = (Button) findViewById(R.id.btn_click);
        tv = (TextView)findViewById(R.id.tvShow);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent( MainActivity.this
                        SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    public void onEventMainThread(FirstEvent event) {
        String msg = "onEventMainThread收到了消息:" + event.getMsg();
        Log.d("harvic", msg);
        tv.setText(msg);
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

SecondActivity:

public class SecondActivity extends Activity {
    private Button btn_FirstEvent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);

        btn_FirstEvent.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                EventBus.getDefault().post(
                        new FirstEvent("FirstEvent btn clicked"));
            }
        });
    }
}

注意:

EventBus.getDefault().post(
new FirstEvent(“FirstEvent btn clicked”));

发布事件:FirstEvent是自己定义的一个类
public class FirstEvent {

private String mMsg;
public FirstEvent(String msg) {
      // TODO Auto-generated constructor stub
      mMsg = msg;
}
public String getMsg(){
      return mMsg ;
}

}

接收订阅事件也要对应的匹配参数
public void onEventMainThread(FirstEvent event)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
abp-vnex eventbus是一个用于在ABP框架中进行事件通信的模块。要使用abp-vnex eventbus,您需要按照以下步骤进行安装和配置: 1. 首先,您需要安装abp-vnex eventbus模块。可以通过运行以下命令来安装: ```shell npm install abp-vnex-eventbus --save ``` 2. 安装完成后,您需要在您的应用程序的模块中导入abp-vnex eventbus模块。在您的模块文件中,添加以下代码: ```typescript import { AbpVnexEventBusModule } from 'abp-vnex-eventbus'; @NgModule({ imports: [ AbpVnexEventBusModule ] }) export class YourModule { } ``` 3. 现在,您可以在您的组件或服务中使用abp-vnex eventbus来发送和接收事件。首先,您需要导入`AbpVnexEventBusService`: ```typescript import { AbpVnexEventBusService } from 'abp-vnex-eventbus'; ``` 4. 在您的组件或服务中,您可以使用`AbpVnexEventBusService`的`emit`方法来发送事件。例如,发送一个名为`myEvent`的事件: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } sendEvent() { this.eventBus.emit('myEvent', { data: 'Hello World' }); } ``` 5. 要接收事件,您可以使用`AbpVnexEventBusService`的`on`方法。在您的组件或服务中,添加以下代码: ```typescript constructor(private eventBus: AbpVnexEventBusService) { } ngOnInit() { this.eventBus.on('myEvent').subscribe((eventData) => { console.log(eventData.data); // 输出:Hello World }); } ``` 这样,您就可以使用abp-vnex eventbus模块来进行事件通信了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值