赞
踩
EventBus的简介
官网是对EventBus给出的解释如下:
/**
* EventBus is a central publish/subscribe event system for Android. Events are posted ({@link #post(Object)}) to the
* bus, which delivers it to subscribers that have a matching handler method for the event type. To receive events,
* subscribers must register themselves to the bus using {@link #register(Object)}. Once registered, subscribers
* receive events until {@link #unregister(Object)} is called. Event handling methods must be annotated by
* {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter
* (the event).
*/
大体的意思是:EventBus是Android的中央发布/订阅事件系统。事件通过post()方法被发布到总线,并将其(事件)传递给与事件类型匹配的处方的订阅者当订阅者需要接收事件时,订阅者必须通过register()方法将自己注册到总线。完成注册后,订阅者就可以接收打牌事件,直到订阅者调用unregister()方法。订阅者用于事件处理方法必须用Subscribe进行注释,而且该方法必须是必须是公用的(public),没有返回值(void),并且只有一个参数(事件)方法。
在build.gradle引入'org.greenrobot:eventbus:3.0.0'
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
}
package com.ymsoft.eventbustest; /** * Created by Xiongxl on 2017/2/24. */ public class FirstEvent { private String mMsg; public FirstEvent(String msg) { mMsg = msg; } public String getMsg() { return mMsg; } }
public class MainActivity extends AppCompatActivity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn_next); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:orientation="vertical">
<Button
android:id="@+id/btn_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="跳转界面2"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
public class SecondActivity extends AppCompatActivity { private Button btn_FirstEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:orientation="vertical">
<Button
android:id="@+id/btn_first_event"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Event"/>
</LinearLayout>
EventBus.getDefault().register(this);当MainActivity停止时,需要注销EventBus,重写onStop()方法
@Override
protected void onStop()
{
super.onStop();
EventBus.getDefault().unregister(this);
}
在secondActivity的Button控件的点击事件中,使用EventBus的post方法,将消息传递给MainActivity。并将消息内容赋值给MainActivity中TextView的Text属性。
在MainActivity中,需要有一个方法,接收由SecondActivity发送过来的消息。btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked")); } });
假如方法没有使用Subscribe注释,将报如下错误:@Subscribe public void onEventMainThread(FirstEvent event) { String msg = "获取到了SecondActivity发送过来的信息:" + event.getMsg(); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); }
EventBus3.0有以下四种ThreadMode:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventMainThread(SecondEvent event)
{
String msg = "SecondEvent onEventMainThread收到了消息:" + event.getMsg();
tv.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
注:接收消息的方法的参数(事件)名称与post()中的参数(事件)一致,否则,将接收不到post传递的消息。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。