EventBus的基本使用

来源:互联网 发布:二维数组元素的排序 c 编辑:程序博客网 时间:2024/06/05 04:53

    这几天有时间整理一下eventbus的使用方法。

使用步骤:A跳转到B , B发消息A接收。

1.添加jar到libs文件加下

2.注册EventBus.getDefault().register(this);

3.解注册EventBus.getDefault().unregister(this);(在注册的时候记得解注册)

@Overrideprotected void onDestroy() {    super.onDestroy();    EventBus.getDefault().unregister(MainActivity.this);}

4.构造发送类

public class AnyEventBus {    private String mMsg;    public AnyEventBus(String msg){       mMsg = msg;    }    public String getMsg(){        return mMsg;    }}

5.发布消息

EventBus.getDefault().post(new AnyEventBus("123"));

6.接收消息()

ThreadMode.MAIN 表示这个方法在主线程中执行

ThreadMode.BACKGROUND 表示该方法在后台执行,不能并发处理

ThreadMode.ASYNC 也表示在后台执行,可以异步并发处理。

ThreadMode.POSTING 表示该方法和消息发送方在同一个线程中执行

@Subscribe(threadMode = ThreadMode.MAIN)public void onEventMainThread(AnyEventBus event) {    String a = event.getMsg();    tv.setText(a);}
完整代码:

A界面
<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.mianshi.haohankwji.meventbus.MainActivity"><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:text="A界面"    android:id="@+id/textView" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="跳转到B界面"        android:id="@+id/button"        android:layout_below="@+id/textView"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" />    <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/button"        android:layout_centerHorizontal="true"        android:layout_marginTop="33dp" /></RelativeLayout>

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    EventBus.getDefault().register(MainActivity.this);//注册eventbus    button = (Button) findViewById(R.id.button);    tv = (TextView) findViewById(R.id.tv);    button.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            Intent intent = new Intent(MainActivity.this, FirstActivity.class);            startActivity(intent);        }    });}/** * 接受消息 * @param event */@Subscribe(threadMode = ThreadMode.MAIN)public void onEventMainThread(AnyEventBus event) {    String a = event.getMsg();    tv.setText(a);}@Overrideprotected void onDestroy() {//解注册    super.onDestroy();    EventBus.getDefault().unregister(MainActivity.this);}

B界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:text="B界面"/>    <Button        android:id="@+id/button_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="给上A界面发送“123" />    <Button        android:id="@+id/button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="跳到C界面并发送”456" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv"/></LinearLayout>

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_first_event);    button_1 = (Button) findViewById(R.id.button_1);    button_2 = (Button) findViewById(R.id.button_2);    tv = (TextView) findViewById(R.id.tv);    button_1.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            EventBus.getDefault().post(new AnyEventBus("123"));            finish();        }    });}public void onEventMainThread(AnyEventBus event) {    String a = event.getMsg();    tv.setText(a);}


消息类
public class AnyEventBus {    private String mMsg;    public AnyEventBus(String msg){        mMsg = msg;    }    public String getMsg(){        return mMsg;    }}

粘性事件:B界面跳转到C界面并传递数据

1.构造信息发送类

2.发布消息EventBus.getDefault().postSticky(new StickyEvent("456"));

3.接收消息

4.注册

5.解注册

完整代码:

消息类
public class StickyEvent {    public String msg;    public StickyEvent(String msg) {        this.msg = msg;    }}

B界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"><TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:gravity="center"    android:text="B界面"/>    <Button        android:id="@+id/button_1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="给上A界面发送“123" />    <Button        android:id="@+id/button_2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="跳到C界面并发送”456" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv"/></LinearLayout>

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_first_event);    button_1 = (Button) findViewById(R.id.button_1);    button_2 = (Button) findViewById(R.id.button_2);    tv = (TextView) findViewById(R.id.tv);    button_2.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            EventBus.getDefault().postSticky(new StickyEvent("456"));            Intent intent = new Intent(FirstActivity.this,SecandActivity.class);            startActivity(intent);        }    });}public void onEventMainThread(AnyEventBus event) {    String a = event.getMsg();    tv.setText(a);}

C界面
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="C界面" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:text="接收" />    <TextView        android:id="@+id/tv"        android:layout_width="match_parent"        android:layout_height="wrap_content" /></LinearLayout>

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_secand_event);    tv = (TextView) findViewById(R.id.tv);    button2 = (Button) findViewById(R.id.button2);    initciew();}private void initciew() {    button2.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            if (sign){                sign = false;//注册一次就可以,避免点击一次注册一次                EventBus.getDefault().register(SecandActivity.this);//注册eventbus            }        }    });}@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)public void onEvent(StickyEvent event) {    String a = event.msg;    tv.setText(a);}@Overrideprotected void onDestroy() {    super.onDestroy();    EventBus.getDefault().removeAllStickyEvents();    EventBus.getDefault().unregister(SecandActivity.this);}

1 0
原创粉丝点击