Android中开源库EventBus使用详解

来源:互联网 发布:英国埃塞克斯大学 知乎 编辑:程序博客网 时间:2024/06/01 21:53

当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,例如:当某一条件发生时,应用中有几个部件对这个消息感兴趣,那么我们通常采用的就是观察者模式,使用观察者模式有一个弊病就是部件之间的耦合度太高,在这里我将会详细介绍Android中的解耦组建EventBus的使用。


EvnetBus的下载地址:https://github.com/greenrobot/EventBus.git


一、使用EventBus的步骤:


1、下载EventBus

2、让自己的项目以来EventBus

3、自定义一个事件(不需要继承任何类),通常我比较喜欢定义一个Message类

4、定义回调函数,相当于观察者模式中的on***Listener函数,在EventBus中可以定义四种类型的回调函数:

a、onEvent   它和ThreadModel中的PostThread对应,这个也是默认的类型,当使用这种类型时,回调函数和发起事件的函数会在同一个线程中执行

b、onEventMainThread,当使用这种类型时,回调函数会在主线程中执行,这个在Android中非常有用,因为在Android中禁止在子线程中修改UI

c、onEventBackgroundThread,当使用这种类型时,如果事件发起函数在主线程中执行,那么回调函数另启动一个子线程,如果事件发起函数在子线程执行,那么回调函数就在这个子线程执行。

d、onEventBusAsync,当使用这种类型时,不管事件发起函数在哪里执行,都会另起一个线程去执行回调。


[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public class MainActivity extends Activity {  
  2.     private ImageView img1;  
  3.     private ImageView img2;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9. //      img1=(ImageView)this.findViewById(R.id.img1);  
  10. //      img2=(ImageView)this.findViewById(R.id.img2);  
  11.         Log.v("EventBus1", Thread.currentThread().getId()+"+++");  
  12.           
  13.         //注册  
  14.         EventBus.getDefault().register(this);  
  15.         EventBus.getDefault().register(new MyClass());  
  16.           
  17.     }  
  18.   
  19.     @Override  
  20.     public boolean onCreateOptionsMenu(Menu menu) {  
  21.         // Inflate the menu; this adds items to the action bar if it is present.  
  22.         getMenuInflater().inflate(R.menu.main, menu);  
  23.         return true;  
  24.     }  
  25.       
  26.     //分发  
  27.     public void postEvent(View view)  
  28.     {  
  29.           
  30.                       
  31.                     EventBus.getDefault().post(new ChangeImgEvent(1));  
  32.                   
  33.     }  
  34.       
  35.     @Override  
  36.     protected void onStop() {  
  37.         // TODO Auto-generated method stub  
  38.         super.onStop();  
  39.         EventBus.getDefault().unregister(this);  
  40.     }  
  41.       
  42.     public void onEventAsync(ChangeImgEvent event)  
  43.     {  
  44.         Log.v("EventBus1", Thread.currentThread().getId()+"----");  
  45.         if(event.getType()==1)  
  46.         {  
  47.             System.out.println("-------------+++++++++++");  
  48.             try {  
  49.                 Thread.sleep(10000);  
  50.             } catch (InterruptedException e) {  
  51.                 // TODO Auto-generated catch block  
  52.                 e.printStackTrace();  
  53.             }  
  54.               
  55.         }  
  56.     }  
  57.       
  58.       
  59.   
  60. }  

具体使用如上,代码比较简单,就不做介绍了,我已经上传详细代码

下载地址:http://download.csdn.net/detail/yuanzeyao2008/6675583

0 3
原创粉丝点击