EventBus

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

基本用法就這麼多

相關文章
相關標籤/搜索