EventBus(二)

来源:互联网 发布:林俊杰 小酒窝 知乎 编辑:程序博客网 时间:2024/06/05 17:04

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

<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;@Overrideprotected 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() {@Overridepublic 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());}@Overrideprotected 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;@Overrideprotected 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() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));}});btn_SecondEvent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new SecondEvent("SecondEvent btn clicked"));}});btn_ThirdEvent.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {EventBus.getDefault().post(new ThirdEvent("ThirdEvent btn clicked"));}});}}


0 0
原创粉丝点击