事件總線EventBus

什麼是事件總線管理?java

  1. 將事件放到隊列裏,用於管理和分發;
  2. 保證應用的各個部分之間高效的通訊及數據,事件分發;
  3. 模塊間解耦;

什麼是EventBus?android

  1. EventBus是發佈/訂閱的事件總線。EventBus模式-也被稱爲MessageBus或者發佈者/訂閱者(publisher/subcriber)模式——能夠讓兩個組件相互通訊,可是他們之間並不相互知曉。
  2. 基於事件總線管理/訂閱/分發模式的。事件響應有更多的線程選擇,EventBus能夠向不一樣的線程中發佈事件。EventBus支持Sticky Event。
  3. 使用時須要先註冊訂閱,而後向訂閱者分發消息數據便可。包含4個成分:發佈者,訂閱者,事件,總線。訂閱者能夠訂閱多個事件,發佈者能夠發佈任何事件,發佈者同時能夠也是訂閱者。分訂閱、註冊、發佈、取消註冊等步驟。git

    以下圖:
    這裏寫圖片描述github

EventBus.getDefault().register(this); //註冊事件 EventBus.getDefault().post(object); //發送事件 EventBus.getDefault().unregister(this); //取消事件 

//訂閱處理事件的方法和區別markdown

//UI線程執行 public void onEventMainThread() {} //當前發佈事件的線程執行 public void onEventPostThread() {} //若是當前非UI線程,則直接調用;若是是UI線程,則將任務加入到後臺的一個隊列 public void onEventBackgroundThread() {} //加入後臺任務隊列,使用線程池調用 public void onEventAsyncThread() {}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

取消註冊:app

EventBus.getDefault().unregister(this); 
  • 1

寫個例子:單擊一個按鈕發送消息,事件在哪裏註冊了,就在哪裏接收:ide

首先在build.gradle里加入一個依賴庫:compile ‘org.greenrobot:eventbus:3.0.0’佈局

package com.example.zhiwenyan.eventbus; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class MainActivity extends AppCompatActivity { private Button btn; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); //註冊事件 btn = (Button) findViewById(R.id.button); textView = (TextView)findViewById(R.id.textView); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MyEvent myEvent = new MyEvent(); myEvent.setType("1"); myEvent.setContent("我是發送的內容"); //發送事件 EventBus.getDefault().post(myEvent); } }); } //處理事件 @Subscribe public void onEventMainThread(MyEvent myEvent) { if (myEvent.getType().equals("1")) { textView.setText(myEvent.getContent()); } } @Override protected void onDestroy() { super.onDestroy(); //取消註冊 EventBus.getDefault().unregister(this); } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 2
  • 25

在OnCreate()裏註冊了一個事件 EventBus.getDefault().register(this);this表明在當前的類註冊,單擊一個按鈕時發佈一個事件EventBus.getDefault().post(myEvent); myEvent事件類的MyEvent的對象。而後在UI線程執行處理了這個事件;最後在onDestory摧毀了這個事件post

事件類:學習

public class MyEvent { private String type; private String content; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

佈局文件就不復制了:一個Button、一個TextView很簡單;

事件分發總庫EventBus,小編正在學習這個,寫一篇博客,幫助你們入門一下;小編認爲學習這個,先把流程搞明白,而後在再去理解,多寫幾個例子(好比在Fragment之間傳遞信息,後臺到前臺傳遞消息等等),最後呢能夠深刻的研究一下里面的源碼(正在研究中….);

你們也能夠在github上去看一下:https://github.com/greenrobot/EventBus

相關文章
相關標籤/搜索