EventBus(二)
2015-10-16 17:47
316 查看
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
demo含义:
第一个界面,跳转到第二个界面,在第二个界面有3个按钮,点击发送消息,在第一个界面进行接受,3个按钮对应3种不同的消息类型
activity_main.xml
activity_second.xml
FirstEvent
SecondEvent
ThirdEvent
MainActivity
SecondActivity
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
demo含义:
第一个界面,跳转到第二个界面,在第二个界面有3个按钮,点击发送消息,在第一个界面进行接受,3个按钮对应3种不同的消息类型
activity_main.xml
<LinearLayout 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:orientation="vertical"> <Button android:id="@+id/btn_try" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn_bty"/> </LinearLayout>
activity_second.xml
<LinearLayout 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:orientation="vertical" tools:context="com.harvic.try_eventbus_1.SecondActivity" > <Button android:id="@+id/btn_first_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Event"/> <Button android:id="@+id/btn_second_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Second Event"/> <Button android:id="@+id/btn_third_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Third Event"/> </LinearLayout>
FirstEvent
package com.harvic.other; public class FirstEvent { private String mMsg; public FirstEvent(String msg) { mMsg = msg; } public String getMsg(){ return mMsg; } }
SecondEvent
package com.harvic.other; public class SecondEvent{ private String mMsg; public SecondEvent(String msg) { mMsg = "MainEvent:"+msg; } public String getMsg(){ return mMsg; } }
ThirdEvent
package com.harvic.other; public class ThirdEvent { private String mMsg; public ThirdEvent(String msg) { mMsg = msg; } public String getMsg(){ return mMsg; } }
MainActivity
package com.harvic.tryeventbus2; import com.harvic.other.FirstEvent; import com.harvic.other.SecondEvent; import com.harvic.other.ThirdEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } /** * 接受事件在UI线程操作 * @param event */ public void onEventMainThread(FirstEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } /** * 接受事件在UI线程操作 * @param event */ public void onEventMainThread(SecondEvent event) { Log.d("harvic", "onEventMainThread收到了消息:" + event.getMsg()); } /** * 接受事件在子线程操作 * @param event */ public void onEventBackgroundThread(SecondEvent event){ Log.d("harvic", "onEventBackground收到了消息:" + event.getMsg()); } /** * 创建新的子线程在执行onEventAsync * @param event */ public void onEventAsync(SecondEvent event){ Log.d("harvic", "onEventAsync收到了消息:" + event.getMsg()); } /** * 发布事件和接收事件线程在同一个线程 */ public void onEvent(ThirdEvent event) { Log.d("harvic", "OnEvent收到了消息:" + event.getMsg()); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }
SecondActivity
package com.harvic.tryeventbus2; import com.harvic.other.FirstEvent; import com.harvic.other.SecondEvent; import com.harvic.other.ThirdEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { private Button btn_FirstEvent, btn_SecondEvent, btn_ThirdEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_SecondEvent = (Button) findViewById(R.id.btn_second_event); btn_ThirdEvent = (Button) findViewById(R.id.btn_third_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked")); } }); btn_SecondEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post( new SecondEvent("SecondEvent btn clicked")); } }); btn_ThirdEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post( new ThirdEvent("ThirdEvent btn clicked")); } }); } }
相关文章推荐
- Oracle中insert into select from用法、table (fn_split())用法、select中的as扩展替代字段用法、Oracle序列
- WINDOWS消息和窗口简介
- javaweb开发错误笔记总结
- 20151015企业招工与县城工业产值增长率问题
- 在Spring环境中建立JPA Spring JPA 提供了三种方法创建JPA EntityManagerFactory:
- Meet-in-the-middle思想的一些应用
- 让Node模块在浏览器运行----browserify
- Adroid记录
- java 获取字符串安全截取长度
- Make sure that `gem install libv8 -v '3.16.14.3'` succeeds before bundling.
- ubuntu adb 连接不弹出授权对话框问题
- 利用Jquery实现页面上div的拖动及位置保存
- $.ajax()方法参数总结
- 获取android SDCard存储大小
- iOS开发009 iOS快速简单集成微信QQ微博分享
- 为什么要写用户体验
- 使用dnsmasq搭建wifi热点DHCP服务器的最简方法
- 100 年内,谁将统一人类走向宇宙?(深度、烧脑)
- C#学习之GDI + 图形编程基础(一)
- iOS开发008 无限循环自动图片轮播器