Android EventBus框架

//EventBus  Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如网络请求等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus来实现

//作为消息总线,有三个主要元素
    /*
        1. 事件    
            可以是任意类型的对象
        2. 事件订阅者,接收特定的事件
            所有事件订阅都都是以onEvent开头的函数,具体来说,函数的名字是onEvent,onEventMainThread,onEventBackgroundThread,onEventAsync这四个
        3. 事件发布者,用于通知Subscribe有事件发生
            可以在任意线程任意位置发送事件,直接调用EventBus的`post(Object)`方法,可以自己实例化EventBus对象,但一般使用默认的单例就好了:`EventBus.getDefault()`,根据post函数参数的类型,会自动调用订阅相应类型事件的函数。
    */

    /*
        Subscriber函数的名字只能是那4个,因为每个事件订阅函数都是和一个`ThreadMode`相关联的,ThreadMode指定了会调用的函数。

            PostThread: 事件的处理和事件的发送在相同的进程,对应的函数名是onEvent

            MainThread: 事件的处理会在UI线程中执行,对应的函数名是onEventMainThread

            BackgroundThread: 事件的处理会在一个后台线程中执行,对应的函数名是onEventBackgroundThread    但是事件处理的时间不应该太长,否则会阻塞后面的事件的派发或处理。

            Async: 事件的处理会在单独的线程中执行    主要用于后台线程中执行耗时操作,每个事件会开启一个线程(有线程池),但最好限制线程的数目

        //主要看事件的处理是在哪里进行,则调用相应的事件订阅
    */



    /*
        使用EventBus四个步骤:
            1、定义一个事件
            2、注册一个订阅者
            3、发布一个事件
            4、接受这个事件
    */
//定义事件
        public class MyEvent{
            private static final String TAG="MyEvent";
            private String msg;
            public MyEvent(String msg){
                this.msg= msg;
            }
        }
//注册订阅者和发布一个事件
        public class MainActivity extends Activity{

            private static final String TAG="MainActivity";
            private TextView mTextView;
            private Button mButton;

            protected void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                mButton = (Button)findViewById(R.id.button1);
                mTextView = (TextView)findViewById(R.id.textView1);

                //注册订阅者
                EventBus.getDefault().register(this);
                mButton.setOnClickListener(new OnClickListener(){
                    public void onClick(View v){
                        //post事件  发送事件 
                        EventBus.getDefault().post(new MyEvent());
                    }
                });

                //接受事件的回调
                public void onEvent(MyEvent event){
                    Log.e("What","[onEvent]My Thread is "+Thread.currentThread().getName());
                }
            }


            protected void onDestroy(){
                super.onDestroy();
                try{
                    EventBus.getDefault().unregister(this);
                }catch(Throwable t){
                    //this may crash registration did not go throught.just be safe
                }
            }

        }
    //上述只有onEvent()事件的处理    
//onEventMainThread使用演示

        public class EventMainActivity extends Activity{

            private TextView mTextView;
            private Button mButton;

            protected void onCreate(Bundle savedInstanceState){
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                mButton = (Button)findViewById(R.id.button1);
                mTextView = (TextView)findViewById(R.id.textView1);
                //注册订阅者
                EventBus.getDefault().register(this);
                mButton.setOnClickListener(new OnClickListener(){
                    public void onClick(View view){
                        new Thread(new Runnable(){
                            public void run(){
                                EventBus.getDefault().post(new MyEvent());
                            }
                        }).start();
                    }
                });
            }
            //会放在发送事件的那个线程中执行,其实不能进行UI更新操作的
            public void onEvent(MyEvent event){
                Log.e("What","[onEvent] My Thread is "+Thread.currentThread().getName());
            }
            //会放到主线程去执行的事件处理
            public void onEventMainThread(MyEvent event){
                Log.e("What","[onEventMainThreadMy] Thread is "+Thread.currentThread().getName());
                mTextView.setText("onEventMainThread called");
            }       
        }
        /*
            Sticky Event:
                Sticky Event是指我注册一个sticky的订阅,这样注册之前发送的sticky事件的最近的一个会保存在内存中,即使错过了这个事件的发送的情况下,也可以通过getStickyEvent收到。

                    postSticky(event):发送sticky的event registerSticky(subscriber):注册接收sticky事件的订阅者 getStickyEvent(Class)
        */

        public class MyEvent{
            public String what;
            public MyEvent(String what){
                super();
                this.what= what;
            }
        }

        public class MainActivity extends Activity{

            private TextView mTextView;
            private Button sendButton;
            private Button queryButton;

            protected void onCreate(Bundle savedInstanceState){
                super.onCreate(savecInstanceState);
                setContentView(R.layout.activity_main);
                sendButton = (Button)findViewById(R.id.button1);
                queryButton = (Button)findViewById(R.id.button2);
                mTextView = (TextView)findViewById(R.id.textView1);

                //注册订阅者
                EventBus.getDefault().register(this);
                sendEvent.setOnClickListener(new OnClickListener(){
                    public void onClick(View view){
                        //点击按键发送5个Sticky Evnet
                        for(itn i=0;i<5;i++){
                            EventBus.getDefault().postSticky(new MyEvent(String.valueOf(i)));
                        }
                    }
                });
                //查询内存中存储的最近发送的事件
                queryButton.setOnClickListener(new OnClickListener(){
                    public void onClick(View view){
                        MyEvent lastEvent = EventBus.getDefault().getStickyEvent(MyEvent.class);
                        Log.e("What","[queryEvent] last Event is "+lastEvent.what);
                    }
                });

                public void onEvent(MyEvent event){
                    Log.e("What","[onEvent] this Event is "+event.what);
                }
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值