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
- Eventbus的基本使用
- EventBus的基本使用
- EventBus的基本使用
- Android EventBus的基本使用
- EventBus的基本使用及原理
- Android的EventBus和butterknife基本使用
- Android EventBus 基本使用
- EventBus基本使用简介
- eventbus基本使用
- EventBus基本使用
- EventBus基本使用
- Android:EventBus-3 基本使用
- 深入理解EventBus - 基本使用
- Android事件总线EventBus 3.0的基本使用
- Android事件总线EventBus 3.0的基本使用
- EventBus 《二》 Android EventBus的简单使用
- EventBus的使用初试
- EventBus的使用
- Ajax学习笔记
- js call()方法
- ARM:裸板shell编程完善(匹配命令、修复bug)、G-Sensor重力传感器驱动基础
- Shader(顶点片段着色器)
- MVC演化、理解
- EventBus的基本使用
- 小鑫の日常系列故事(十)——排名次 (sdut oj)
- 快速上手Vue(适合懂基础html、css和js的人)
- 面试-webservice
- 自己刚学时候的链表整理一下,方便以后翻阅
- iOS 轮播视图的实现方式 和 RunLoopMode 的注意
- Android布局
- 求二叉树的深度
- Performing summary statistics and plots —— Python Data Science Cookbook