EventBus的基本使用

     EventBus是一款本地组件间通信框架。在大型项目的Activities,fragments,Threads,Services都可以看到它的使用场景,尽管EventBus在向未创建的组件传递事件时有些局限,仅适合在“活着的”组件间传递消息,但仍不妨碍它活跃在各个大型项目各个场景里。
     使用EventsBus,因为其和GreenDAO出自一家公司,并且使用它非常的简单,所以现在很多的互联网app都会使用EventsBus来进行消息传递。
     EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。
     
1、基本用法
     添加支持:添加依赖库:compile 'de.greenrobot:eventbus:3.0.0-beta1'
     或直接添加.jar库:eventbus-3.0.0.jar

2、注册
举个例子,你需要在一个activity中注册eventbus事件,然后定义接收方法,这和Android的广播机制很像,你需要首先注册广播,然后需要编写内部类,实现接收广播,然后操作UI,在EventBus中,你同样需要这么做。
@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EventBus.getDefault().register(this);

}
@Overrideprotected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}
3、订阅者
/**
 *  事件接收必须是 public
 *  否则报异常: its super classes have no public methods with the @Subscribe annotation
 *  @param  //String
 */
@Subscribe( threadMode = ThreadMode. MAIN)
public void  helloEventBus(String message){
    Log. e( "xushao" , " 通知输出的信息 :"+message) ;
}
     该操作很简单,定义了一个hello方法,需要传入String参数,在其中操作UI操作,注意:我们添加了注解@Subscribe,其含义为订阅者,在其内传入了threadMode,我们定义为ThreadMode.MainThread,其含义为该方法在UI线程完成,这样你就不要担心抛出异常啦。是不是很简单?

4、发布者
     既然你在某个地方订阅了内容,当然就会在某个地方发布消息。举个例子,你的这个activity需要http请求,而http请求你肯定是在异步线程中操作,其返回结果后,你可以这么写:
String json="";
EventBus.getDefault().post(json);
5、原理初探
     你订阅了内容,所以你需要在该类注册EventBus,而你订阅的方法需要传入String,即你的接收信息为String类型,那么在post的时候,你post出去的也应该是String类型,其才会接收到消息。

==如果你post的是对象:
首先你需要定义一个类似pojo类:
public class MessageEvent {
  public final String name;
  public final String password;
  public MessageEvent(String name,String password) {
    this.name = name;
    this.password=password;
  }
}

然后你post的时候:
EventBus.getDefault().post(new MessageEvent("hello","world"));

当然,你接收的方法也需要改为:
@Subscribe(threadMode = ThreadMode.MainThread)
public void helloEventBus(MessageEvent message){
    mText.setText(message.name);
}
6、3.0新增的@Subscribe

                              
                                                  二、具体的代码演示
1、事件实体类:

/**
 * Created by xuyangsheng on 2017/6/26.
 * Author : xuyangsheng
 * Time : 2017/6/26 10:36
 * Name :
 * OverView :
 * Usage :  事件传递类对象
  */

public class MessageEvent {
    public final String  name ;
    public final String  password ;
    public  MessageEvent(String name ,String password) {
        this. name = name ;
        this. password=password ;
    }
}

2、订阅消息接收者:


public class MainActivity  extends Activity {

    private Button  btn_eventbus_1 ;
    private Button  btn_eventbus_2 ;

    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_main) ;
        EventBus. getDefault().register( this) ;
        initView() ;
        initListener() ;
    }

    private void  initView() {
        btn_eventbus_1 = (Button) findViewById(R.id. btn_eventbus_1) ;
        btn_eventbus_2 = (Button) findViewById(R.id. btn_eventbus_2) ;
    }

    private void  initListener() {
        btn_eventbus_1.setOnClickListener( new View.OnClickListener() {
            @Override
            public void  onClick(View pView) {
                startActivity( new Intent(MainActivity. this,SecondActivity. class)) ;
            }
        }) ;
        btn_eventbus_2.setOnClickListener( new View.OnClickListener() {
            @Override
            public void  onClick(View pView) {
                startActivity( new Intent(MainActivity. this,ThirdActivity. class)) ;
            }
        }) ;
    }

    @Override
    protected void  onDestroy() {
        super.onDestroy() ;
        EventBus. getDefault().unregister( this) ;
    }

    /**
     *  事件接收必须是 public
     *  否则报异常: its super classes have no public methods with the @Subscribe annotation
     *  @param  //String
     */
    @Subscribe( threadMode = ThreadMode. MAIN)
    public void  helloEventBus(String message){
        Log. e( "xushao" , " 通知输出的信息 :"+message) ;
    }

    @Subscribe( threadMode = ThreadMode. MAIN)
    public void  helloEventBus(MessageEvent message){
        Log. e( "xushao" , " 通知输出的信息 :message.name:"+message. name+ "------message.password:"+message. password) ;
    }
   
}

3、订阅消息简单发送者

/**
 * Created by xuyangsheng on 2017/6/26.
 * Author : xuyangsheng
 * Time : 2017/6/26 10:09
 * Name :
 * OverView :
 * Usage :
 */

public class SecondActivity  extends Activity {

    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activtiy_second) ;
        String  json= "Created by xuyangsheng on 2017/6/26.xuyangsheng" ;
        EventBus. getDefault().post( json) ;
    }
}


4、订阅消息(实体对象)发送者

/**
 * Created by xuyangsheng on 2017/6/26.
 * Author : xuyangsheng
 * Time : 2017/6/26 10:38
 * Name :
 * OverView :
 * Usage :
 */

public class ThirdActivity  extends Activity {

    @Override
    protected void  onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState) ;
        setContentView(R.layout. activity_third) ;

        EventBus. getDefault().post( new MessageEvent( "hello" , "world")) ;
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值