EventBusgit
GitHub 上的地址 https://github.com/greenrobot/EventBusgithub
EventBus干的事情能够归纳来讲 别人能够根据某一个事件订阅我,可是他得去实现若是我发生了这个event,我该去怎么作。因此发生这个事件的时候,全部的订阅了这个事件的订阅者都应该去执行我实现的相应的操做。那么我怎么知道发生了这个event,何时去执行呢,这就是eventbus要干的事情了。
因此说Eventbus能够完成线程于线程之间的通讯,某些状况下相比于handler,要好用的的多,也更简单。async
先介绍一下EventBus的用法:ide
首先你须要定义一个Event,这个是一个class, 能够随便定义,这个class就是一个事件,你可能须要这个事件去携带你想要告诉别人的信息的事件,
执行者就须要根据你这个事件来执行一些定义的操做。post
你还须要一个执行者,就是注册了这个事件的执行者,和一个通知者。ui
<1>this
public class FirstEvent {
private String content; public FirstEvent(String content) { this.content = content; } public String getContent() { return content; } }
这只是一个单纯的event,没有实际的意义spa
<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) {code
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
基本用法就这么多