一、概述
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
使用步骤:
(1)自定义一个类,可以是空类,比如:
1. public class AnyEventType {
2. public AnyEventType(){}
3. }
(2)在要接收消息的页面注册:
1. eventBus.register(this); 在接受消息的页面onCreate方法里进行注册
(3)发送消息
1. eventBus.post(new AnyEventType event);
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
1. public void onEvent(AnyEventType event) {
2. }
(5)解除注册
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
EventBus.getDefault().unregister(this);
}
在onDestroy里进行解除注册。
1. package com.harvic.other;
2.
3. public class FirstEvent {
4.
5. private String mMsg;
6. public FirstEvent(String msg) {
7. // TODO Auto-generated constructor stub
8. mMsg = msg;
9. }
10. public String getMsg(){
11. return mMsg;
12. }
13. }
这个类很简单,构造时传进去一个字符串,然后可以通过getMsg()获取出来。
在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。
通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:
1. package com.example.tryeventbus_simple;
2.
3. import com.harvic.other.FirstEvent;
4.
5. import de.greenrobot.event.EventBus;
6. import android.app.Activity;
7. import android.content.Intent;
8. import android.os.Bundle;
9. import android.util.Log;
10. import android.view.View;
11. import android.widget.Button;
12. import android.widget.TextView;
13. import android.widget.Toast;
14.
15. public class MainActivity extends Activity {
16.
17. Button btn;
18. TextView tv;
19.
20. @Override
21. protected void onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.activity_main);
24. //注册EventBus
25. EventBus.getDefault().register(this);
26.
27. btn = (Button) findViewById(R.id.btn_try);
28. tv = (TextView)findViewById(R.id.tv);
29.
30. btn.setOnClickListener(new View.OnClickListener() {
31.
32. @Override
33. public void onClick(View v) {
34. // TODO Auto-generated method stub
35. Intent intent = new Intent(getApplicationContext(),
36. SecondActivity.class);
37. startActivity(intent);
38. }
39. });
40. }
41. @Override
42. protected void onDestroy(){
43. super.onDestroy();
44. EventBus.getDefault().unregister(this);//反注册EventBus
45. }
46. }
发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!
1. EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
完整的SecondActivity.Java的代码如下:
1. package com.example.tryeventbus_simple;
2.
3. import com.harvic.other.FirstEvent;
4.
5. import de.greenrobot.event.EventBus;
6. import android.app.Activity;
7. import android.os.Bundle;
8. import android.view.View;
9. import android.widget.Button;
10.
11. public class SecondActivity extends Activity {
12. private Button btn_FirstEvent;
13.
14. @Override
15. protected void onCreate(Bundle savedInstanceState) {
16. super.onCreate(savedInstanceState);
17. setContentView(R.layout.activity_second);
18. btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);
19.
20. btn_FirstEvent.setOnClickListener(new View.OnClickListener() {
21.
22. @Override
23. public void onClick(View v) {
24. // TODO Auto-generated method stub
25. EventBus.getDefault().post(
26. new FirstEvent("FirstEvent btn clicked"));
27. }
28. });
29. }
30. }
接收消息时,我们使用EventBus中最常用的onEventMainThread()函数来接收消息,具体为什么用这个,我们下篇再讲,这里先给大家一个初步认识,要先能把EventBus用起来先。
在MainActivity中重写onEventMainThread(FirstEvent event),参数就是我们自己定义的类:
在收到Event实例后,我们将其中携带的消息取出,一方面Toast出去,一方面传到TextView中;
1. public void onEventMainThread(FirstEvent event) {
2. String msg = "onEventMainThread收到了消息:" + event.getMsg();
3. Log.d("harvic", msg);
4. tv.setText(msg);
5. Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
6. }
完整的MainActiviy代码如下:
1. package com.example.tryeventbus_simple;
2.
3. import com.harvic.other.FirstEvent;
4.
5. import de.greenrobot.event.EventBus;
6. import android.app.Activity;
7. import android.content.Intent;
8. import android.os.Bundle;
9. import android.util.Log;
10. import android.view.View;
11. import android.widget.Button;
12. import android.widget.TextView;
13. import android.widget.Toast;
14.
15. public class MainActivity extends Activity {
16.
17. Button btn;
18. TextView tv;
19.
20. @Override
21. protected void onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.activity_main);
24.
25. EventBus.getDefault().register(this);
26.
27. btn = (Button) findViewById(R.id.btn_try);
28. tv = (TextView)findViewById(R.id.tv);
29.
30. btn.setOnClickListener(new View.OnClickListener() {
31.
32. @Override
33. public void onClick(View v) {
34. // TODO Auto-generated method stub
35. Intent intent = new Intent(getApplicationContext(),
36. SecondActivity.class);
37. startActivity(intent);
38. }
39. });
40. }
41.
42. public void onEventMainThread(FirstEvent event) {
43.
44. String msg = "onEventMainThread收到了消息:" + event.getMsg();
45. Log.d("harvic", msg);
46. tv.setText(msg);
47. Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
48. }
49.
50. @Override
51. protected void onDestroy(){
52. super.onDestroy();
53. EventBus.getDefault().unregister(this);
54. }
55. }
好了,到这,基本上算初步把EventBus用起来了,下篇再讲讲EventBus的几个函数,及各个函数间是如何识别当前如何调用哪个函数的。
使用实例剖析
适用在主线程、交互线程、后台线程
使用的步骤:
使用案例: