EventBus

EventBus

GitHub 上的地址 https://github.com/greenrobot/EventBus


EventBus干的事情可以概括来说 别人可以根据某一个事件订阅我,但是他得去实现如果我发生了这个event,我该去怎么做。所以发生这个事件的时候,所有的订阅了这个事件的订阅者都应该去执行我实现的相应的操作。那么我怎么知道发生了这个event,什么时候去执行呢,这就是eventbus要干的事情了。
所以说Eventbus可以完成线程于线程之间的通信,某些情况下相比于handler,要好用的的多,也更简单。

先介绍一下EventBus的用法:

首先你需要定义一个Event,这个是一个class, 可以随便定义,这个class就是一个事件,你可能需要这个事件去携带你想要告诉别人的信息的事件,
执行者就需要根据你这个事件来执行一些定义的操作。

你还需要一个执行者,就是注册了这个事件的执行者,和一个通知者。

<1>

public class FirstEvent {



private String content;
public FirstEvent(String content)
{
this.content = content;
}

public String getContent() {
return content;
}
}

这只是一个单纯的event,没有实际的意义

<2>

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.main_button);
EventBus.getDefault().register(this);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void changeTextView(FirstEvent event) {


String msg = "changeTextView:" + event.getContent();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}

 



订阅者,EventBus.getDefault().register(this);表示该类进行了注册

最关键的是 changeTextView()这个方法,上面加上注解@Subcrib 表示当FirstEvent发生的时候 才能去执行这个方法,ThreadMode.MAIN 表示在主线程中执行。
<3>

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
button = (Button) findViewById(second_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new FirstEvent("Hello world!"));
}
});
}

发送者,这是另个activity,当你一点击button ,就会EventBus.getDefault().post(new FirstEvent("Hello world!")); 订阅者就会去执行changeTextView()这个方法。


其实EventBus的原理,就是当你注册的时候,会去得到你所有添加了@Subscribe的方法,并以event为key,以封装的method的list为Value,放到一个map中,当postevent的时候,将event根据优先级放到一个队列中,然后从队列中拿出event,去map中找到所以订阅了这个event的Method,最后根据反射去执行这个method。

在添加注解@Subscribe(threadMode = ThreadMode.MAIN) 其中有个mode 这个是一个枚举值

/**
* Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
* implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
* simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
* using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
*/
POSTING, //默认值。表示posting是哪个线程,就在哪个线程中执行

/**
* Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is
* the main thread, event handler methods will be called directly. Event handlers using this mode must return
* quickly to avoid blocking the main thread.
*/
MAIN,// 主线程

/**
* Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
* will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
* background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
* return quickly to avoid blocking the background thread.
*/
BACKGROUND, //如果posting是一个主线程 就开启一个单独的线程

/**
* Event handler methods are called in a separate thread. This is always independent from the posting thread and the
* main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
* use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
* of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
* uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
*/
ASYNC

基本用法就这么多

原文地址:https://www.cnblogs.com/xlurenjia/p/5741408.html