使用otta bus進行android組件通訊

otta 介紹 
java

otta是相似 event bus的 消息通訊包 使用很是簡單android

otta  下載地址 https://dl.dropboxusercontent.com/u/17850028/otto-2.0.0-SNAPSHOT.jar框架

下載後加入 libs 目錄下便可ide

otta 使用方法  
post


public class BusActivity extends FragmentActivity {

  
        BasicBus bus =new BasicBus;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                //須要監聽的bus 須要先註冊
                bus.register(this);
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                 //一樣 用完後要註銷   這些操做可在父類中使用
                bus.unregister(this);
        }
    
    /*
    * 須要監聽的方法加上 @Subscribe便可
    *傳遞的  UpdateEvent 可自定義
    */
    @Subscribe
    public void onUpdate(UpdateEvent event) {
        setTitle(event.title);
    }}


public class SendFragment extends Fragment {

    int counter = 1;

     BasicBus bus =new BasicBus;

    
    /**
    *在一個按鈕的點擊事件中可傳遞消息
    * 消息的提供者 無需註冊
    * post傳遞的是object 徹底可自定義
    **
    /
    void fragmentButtonClicked() {
        bus.post(new UpdateEvent("Clicks: " + counter++));
    }}


 abase android開發框架中 中集成了 otta 可直接使用 Abus 來獲取 使用方法相同this

查看 abase  http://www.oschina.net/p/abase.net

相關文章
相關標籤/搜索