标签:
EventBus 作为 Android 开发中常用的框架,拥有着许多优点:
调度灵活。不依赖于 Context,使用时无需像广播一样关注 Context 的注入与传递。父类对于通知的监听和处理可以继承给子类,这对于简化代码至关重要;通知的优先级,能够保证 Subscriber 关注最重要的通知;粘滞事件(sticky events)能够保证通知不会因 Subscriber 的不在场而忽略。可继承、优先级、粘滞,是 EventBus 比之于广播、观察者等方式最大的优点,它们使得创建结构良好组织紧密的通知系统成为可能。
使用简单。EventBus 的 Subscriber 注册非常简单,调用 eventBus 对象的 register 方法即可,如果不想创建 eventBus 还可以直接调用静态方法 EventBus.getDefault() 获取默认实例,Subscriber 接收到通知之后的操作放在 onEvent 方法里就行了。成为 Publisher 的过程就更简单了,只需要调用合适的 eventBus(自己创建的或是默认的)的 post 方法即可。
快速且轻量。作为 github 的明星项目之一, EventBus 的源代码中许多技巧来改善性能;
下面我们实现这一一个效果,A页面跳转到B页面,B页面发送通知更新A页面的textView的字体颜色(颜色值随机),效果图如下:
放了一个按钮和一个显示文字的TextView,跳转到nextactivity的按钮
<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"
android:gravity="center"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Jump NextActivity" />
<TextView
android:id="@+id/color_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:textSize="18sp"
android:text="这是一段可以被改变字体颜色的文字"
/>
</LinearLayout>
定义事件类,获取颜色和当前的activity
/**
*
* <改变颜色的Event>
*
* @author wangkeke
* @version [V1.00, 2016年5月10日]
* @see [相关类/方法]
* @since V1.00
*/
public class ChangeTextColorEvent
{
private String color;
private Activity activity;
public ChangeTextColorEvent(String color, Activity activity)
{
super();
this.color = color;
this.activity = activity;
}
public String getColor()
{
return color;
}
public Activity getActivity()
{
return activity;
}
}
代码非常简单,注册EventBus,反注册EventBus,onEventMainThread用于接收订阅的事件
public class MainActivity extends Activity
{
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this);
initView();
}
private void initView()
{
button = (Button)findViewById(R.id.btn);
textView = (TextView)findViewById(R.id.color_text);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//跳转到NextActivity
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
}
});
}
/**
* public修饰,不能使用static abstart修饰
* 参数只能有一个
* @param event
* @see [类、类#方法、类#成员]
*/
@Subscribe
public void onEventMainThread(ChangeTextColorEvent event)
{
textView.setTextColor(Color.parseColor(event.getColor()));
//finish NextActivity
event.getActivity().finish();
}
@Override
protected void onDestroy()
{
super.onDestroy();
//反注册EventBus
EventBus.getDefault().unregister(this);
}
}
定义一个发送消息的按钮
<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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16sp"
android:text="发送改变字体颜色的Event" />
</RelativeLayout>
public class NextActivity extends Activity
{
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
initView();
}
private void initView()
{
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(NextActivity.this, getRandColorCode(), Toast.LENGTH_SHORT).show();
//发送消息
EventBus.getDefault().post(new ChangeTextColorEvent(getRandColorCode(), NextActivity.this));
}
});
}
/**
* 获取十六进制的颜色代码.例如 "#6E36B4"
*
* @return String
*/
public static String getRandColorCode()
{
String r, g, b;
Random random = new Random();
r = Integer.toHexString(random.nextInt(256)).toUpperCase();
g = Integer.toHexString(random.nextInt(256)).toUpperCase();
b = Integer.toHexString(random.nextInt(256)).toUpperCase();
r = r.length() == 1 ? "0" + r : r;
g = g.length() == 1 ? "0" + g : g;
b = b.length() == 1 ? "0" + b : b;
return "#"+r + g + b;
}
}
EventBus除了可以使用onEventMainThread()函数接收之外,其实EventBus还有另外几个不同的函数,他们分别是:
1、onEvent
2、onEventMainThread
3、onEventBackgroundThread
4、onEventAsync
四种订阅函数都是使用onEvent开头的,它们的功能稍有不同,在介绍不同之前先介绍两个概念:
告知观察者事件发生时通过EventBus.post函数实现,这个过程叫做事件的发布,观察者被告知事件发生叫做事件的接收,是通过下面的订阅函数实现的。
备注:
参考文章:
标签:
原文地址:http://blog.csdn.net/wangkeke1860/article/details/51363102