EventBus设置TAG,可以更灵活的发送和接收消息

来源:互联网 发布:淘宝卖的黄金是真的吗 编辑:程序博客网 时间:2024/06/06 02:10

EventBus设置TAG

今天和同事谈到了组件通信的EventBus,说到了回调的实现,java代码可以通过注册回调实现被调用者通知调用者,可是EventBus只能来回发消息,感觉有点麻烦,如果发送对象的话还得区分对象,每个事件写一个这很明显是不明智的,上网搜了一下EventBus是否有类似回调的机制,然后就看到了TAG这个东西,类似于msg.what,这样就可以通过泛型、object或者bundle来传递数据了,然后通过TAG来区分,再强转。。。完美~!EventBus用了不到一周,了解的肯定只是片面,如有问题,请大牛指正。原文: https://www.cnblogs.com/cherrylv/p/6433152.html

开始搬运

如果是用Android Studio开发的话,在Gradle文件中 :

compile 'org.simple:androideventbus:1.0.5'

即可使用了。下面贴代码:

public class SubscriberActivity extends AppCompatActivity {    public static final String GET_MESSAGE_SUCCESS ="get_message_success" ;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_subscriber);        if (isUseEventBus()){            EventBus.getDefault().register(this);        }        initView();    }    protected boolean isUseEventBus(){        return true;    }    private  TextView get_msg_tv;    private void initView() {        Button jump_btn = (Button) findViewById(R.id.jump_btn);        jump_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(SubscriberActivity.this,RxJavaActivity.class);                startActivity(intent);            }        });       get_msg_tv = (TextView) findViewById(R.id.get_msg_tv);    }    @Subscriber(tag = GET_MESSAGE_SUCCESS)    private void getMessageSuccess(String msg){        get_msg_tv.setText(msg);    }    @Override    protected void onDestroy() {        super.onDestroy();        if (isUseEventBus()){            EventBus.getDefault().unregister(this);        }    }}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button android:id="@+id/jump_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="jump to RxJavaActivity"/>    <TextView android:id="@+id/get_msg_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="未收到msg的textView"/></LinearLayout>

入口activity:

public class RxJavaActivity extends AppCompatActivity {    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout_rxjava);        initView();    }    private void initView() {        final String msg = "由RxJavaActivity发送给SubscriberActivity的消息";        Button send_msg_btn = (Button) findViewById(R.id.send_msg_btn);        send_msg_btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS);            }        });    }}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <Button android:id="@+id/send_msg_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="send message to activity first"/></LinearLayout>

这是返回msg的Activity。

EventBus使用就是这么简单的一句

EventBus.getDefault().post(msg,SubscriberActivity.GET_MESSAGE_SUCCESS);

参数一是你需要传递过去的数据data,参数二是tag,自定义一个常量字符串即可。

这是发送,那怎么接收呢

@Subscriber(tag = GET_MESSAGE_SUCCESS)    private void getMessageSuccess(String msg){        get_msg_tv.setText(msg);    }

注解Subscriber,tag=上面自定义的字符串常量,方法名自定义(其实没什么作用),而EventBus识别具体从哪个EventBus源(一个项目中肯定有多出Eventbus的post)post过来的通过①tag②方法的参数。

当他检查到tag一致,参数(post方法的第一个参数与自定义方法的参数一致)一致,即能响应。

当然最关键的一步:注册。

EventBus.getDefault().register(this);

这一步必不可少,否则无效。而上面贴的代码里我们自定义了一个方法 isUseEventBus ,当我们开发一个项目的时候必然有一个基类,直接将注册写在基类中,通过这个方法决定你的子类要不要使用EventBus即可。

最后别忘了在 onDestroy 方法中注销EventBus,这是个好习惯(涉及到内存泄漏问题)。

到此我们就会发现不需要你自定义接口,不需要用Handler处理子线程主线程等繁琐的问题,确实比接口回调省事多了。虽然栗子很简单,但是在实际项目中有更大的使用价值。
(纯属搬用,只为做记录,如有侵权请联系,我会及时删除)