一、概述 EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间

一、概述

EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
1、下载EventBus的类库

源码:https://github.com/greenrobot/EventBus

EventBus

EventBus is a publish/subscribe event bus optimized for Android.

EventBus in 3 steps

  1. Define events:

    public class MessageEvent { /* Additional fields if needed */ }//数据domain
  2. Prepare subscribers: Register your subscriber (in your onCreate or in a constructor)://注册订阅者

    eventBus.register(this);

    Declare your subscribing method://自定义订阅者方法 如 xxx

    @Subscribe  
    public void onEvent(AnyEventType event) {/* Do something */};
  3. Post events:

    eventBus.post(event);//发布者发布数据
一下本人试玩写的一个xiaoDemo

1.step

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'
    compile 'org.greenrobot:eventbus:3.0.0'
}
2. a->B 实现a到B页面B界面回传到A界面的数据

A

package com.example.administrator.eventbus30demo;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class MainActivity extends AppCompatActivity {
  TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= (TextView) findViewById(R.id.text);
        EventBus.getDefault().register(this);//接受消息的界面注册
    }
    public void ToNext(View v){
        startActivity(new Intent(this,SecondPage.class));

    }
   //自定义的订阅者方法内部使用注解实现方法名自己定义
    @Subscribe
    public void x(Msg event) {
        String name = event.getName();
        textView.setText(name);
    };
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//销毁时取消订阅者
    }
}
B界面

package com.example.administrator.eventbus30demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import org.greenrobot.eventbus.EventBus;
/**
 * Created by Administrator on 2016/9/5.
 */
public class SecondPage extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondpage);
    }
    public  void ToPervious(View v){
        finish();
        EventBus.getDefault().post(new Msg("gsc com  on !"));//发布者发布事件

    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江南一舟110

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值