EventBus简单介绍:

 EventBus是android下高效的发布订阅事件总线机制,作用是可以代替传统的Intent,Handler,Broadcast或接口函数在Fragment,Activity,Service线程之间传递数据,特点是代码简洁,是一种发布订阅设计模式,或称之为观察者设计模式。

  一般在使用EventBus,在更新发布的内容消息的时候应用比较多,可以及时更新数据。

第一步:导入EventBus需要用到的jar包

compile 'org.greenrobot:eventbus:3.0.0'

第二步:设计

准备两个简单的界面,第一个界面:一个Button和一个隐藏的TextView

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/activity_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context="zhiyuan.eventtest.MainActivity">
     <Button
         android:id="@+id/bt_in"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:text="跳 转"/>
     <TextView
         android:id="@+id/tv_event"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="eventbus使用"
         android:textSize="20sp"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="20dp"
         android:visibility="gone"/>
 </RelativeLayout>

第二个界面:一个Button按钮

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
     android:layout_height="match_parent">
     <Button
         android:id="@+id/bt_for"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_centerInParent="true"
         android:text="回传一个事件,显示MainActivity的textview"
         />
 </RelativeLayout>
 Activity:
 public class MainActivity extends Activity {
     Button btin;
     TextView tvevent;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         //注册BventBus
         EventBus.getDefault().register(this);
         btin= (Button) findViewById(R.id.bt_in);
         tvevent= (TextView) findViewById(R.id.tv_event);
         btin.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 startActivity(new Intent(MainActivity.this,SecondActivity.class));
             }
         });
     }
     //接收secondActivity发送过来msg消息,来改变界面的显示
     @Subscribe
     public void onEventMainThread(Events event) {
         if(event.getMsg()=="events") {
             tvevent.setVisibility(View.VISIBLE);
         }
     }
     @Override
     protected void onDestroy() {
         super.onDestroy();
         //反注册
         EventBus.getDefault().unregister(this);
     }
 }

在这里我用的是onEventMainThread()方法,EventBus里面有四个方法,分别是onEvent,onEventMainThread,onBackground,onEventAnync,这四个方法只要是关于这四个方法是在UI线程还是子线程值进行执行的区别

onEvent():如果使用此方法作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是发布事件和接收事件在同一线程,使用此方法的时候不能执行耗时的操作,如果执行耗时的操作容易导致事件分发延迟。

onEventMainThread():如果会用onEventMainThread作为订阅函数,那么不论事件在那个线程中发布出来的,onEventMainThread都会在UI线程中进行。(在一些其他的网上资料上,都会说智能在UI线程中更新UI,个人感觉这个说法是不对的,只能说在UI线程中更新更加方便,假如我们要在子线程中更新,那么只要我们让子线程拥有一个ViewRoot就可以进行更新的,本片文章的例子一onEventMainThread这个方法,这个方法可以适用于大多数的情况)

onEventBackground():使用此方法作为订阅函数,如果事件实在UI线程中发布出来的,那么此方法就会在子线程中运行,如果事件本来就是子线程发布出来的,那么此方法直接在该子线程中执行。。

onEventAsync():使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.

public class SecondActivity extends Activity{
     Button btfor;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_second);
         btfor= (Button) findViewById(R.id.bt_for);
         btfor.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
              //点击传递一个msg的消息,告诉MainActivty更新UI
                 EventBus.getDefault().post(new Events("events"));
                 finish();
             }
         });
     }
 }

实体消息类:

public class Events {
     //消息实体类
     String msg;
     public Events(String mMsg) {
         // TODO Auto-generated constructor stub
         msg = mMsg;
     }
     public String getMsg() {
         return msg;
     }
 }