EventBus的使用详解
2016-04-29 15:19
399 查看
EventBus的使用详解
Eclipse用户请下载EventBus的源码导入
源码地址:https://github.com/greenrobot/EventBus EventBus是一个Android 端优化的Publish/Subscribe 消息总线,它简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络等网络返回时通过Handler 或Broadcast 通知UI,两个Fragment 之间需要通过Listener通信,这些需求都可以通过 EventBus 实现,最常见的就是它可以代替Intent,HandlerBroadCase 在Activity,Service,Fragment 间传递消息。 Eventbus作为事件总线,它主要有三个元素:1、 Event:事件
2、Subscriber:事件订阅者,接收特定的事件
3、Publisher:事件发布者,用于通知Subscriber有事件发生
基本使用: (1)自定义一个类,可以是空类,比如:public class AnyEventType { public AnyEventType(){} }(2)在要接收消息的页面注册:
eventBus.register(this);(3)发送消息
eventBus.post(new AnyEventType event);(4)接受消息的页面实现(共有四个函数,各功能不同,可以选择性的实现):
public void onEvent(AnyEventType event) {} public void onEventMainThread(AnyEventType event) {} public void onEventBackgroundThread(AnyEventType event) {} public void onEventAsync(AnyEventType event) {}o4000nEvent:如果使用onEvent 作为订阅函数,那么该事件在哪个线程发布出来的,onEvent 就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent 方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。onEventMainThread:如果使用onEventMainThread 作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread 都会在UI 线程中执行,接收事件就会在UI 线程中运行,这个在Android 中是非常有用的,因为在Android 中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。onEventBackground:如果使用onEventBackgrond 作为订阅函数,那么如果事件是在UI 线程中发布出来的,那么onEventBackground 就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground 函数直接在该子线程中执行。onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync。(5)解除注册
eventBus.unregister(this);实战:
1、基本框架搭建
想必大家从一个Activity跳转到第二个Activity的程序应该都会写,这里先稍稍把两个Activity跳转的代码建起来。后面再添加EventBus相关的玩意。MainActivity布局(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"/> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="match_parent"/> </LinearLayout>新建一个Activity,SecondActivity布局(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"/> </LinearLayout>MainActivity.java (点击btn跳转到第二个Activity)
package com.nova.eventbus_1; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class MainActivity extends AppCompatActivity { private Button button_jump; private TextView tv_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_jump= (Button) findViewById(R.id.bt_jump); tv_notify= (TextView) findViewById(R.id.tv_notify); button_jump.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { <strong> Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(intent,1);</strong> } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case 1: if(resultCode==1){ String returnedData=data.getStringExtra("com.nova.eventbus_1.data_return"); tv_notify.setText(returnedData); Toast.makeText(MainActivity.this,returnedData,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); } } } }
<span style="color: rgb(17, 17, 17); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 23.3999996185303px;">SecondActivity.java</span> import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; /** * Created by Nova on 2016/4/11. */ public class SecondActivity extends AppCompatActivity { private Button button_post; private Button button_post2;//使用EventBus传递 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); button_post= (Button) findViewById(R.id.bt_post); button_post2= (Button) findViewById(R.id.bt_post2); button_post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("com.nova.eventbus_1.data_return","跳转按钮:被点击了"); setResult(1,intent); finish(); } }); } } </span>2、新建一个类FirstEvent
package com.harvic.other; public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg(){ return mMsg; } }这个类很简单,构造时传进去一个字符串,然后可以通过getMsg()获取出来。
3、在要接收消息的页面注册EventBus:
在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:package com.nova.eventbus_1; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class MainActivity extends AppCompatActivity { private Button button_jump; private TextView tv_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); <strong> //注册EventBus EventBus.getDefault().register(MainActivity.this);</strong> button_jump= (Button) findViewById(R.id.bt_jump); tv_notify= (TextView) findViewById(R.id.tv_notify); button_jump.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(intent,1); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case 1: if(resultCode==1){ String returnedData=data.getStringExtra("com.nova.eventbus_1.data_return"); tv_notify.setText(returnedData); Toast.makeText(MainActivity.this,returnedData,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); } } } @Subscribe public void onEventMainThread(FirstEvent event){ String msg="onEventMainThread收到了消息:" + event.getMsg(); tv_notify.setText(msg); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); } @Override protected void onDestroy() { super.onDestroy(); <strong>EventBus.getDefault().unregister(MainActivity.this);</strong> } }
4、发送消息
发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!EventBus.getDefault().post(new FirstEvent("跳转按钮:被点击了EventBus post"));完整的SecondActivity.java的代码如下:
package com.nova.eventbus_1; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; /** * Created by Nova on 2016/4/11. */ public class SecondActivity extends AppCompatActivity { private Button button_post; private Button button_post2;//使用EventBus传递 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_two); button_post= (Button) findViewById(R.id.bt_post); button_post2= (Button) findViewById(R.id.bt_post2); button_post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("com.nova.eventbus_1.data_return","跳转按钮:被点击了"); setResult(1,intent); finish(); } }); button_post2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //EventBus使用方法 EventBus.getDefault().post(new FirstEvent("跳转按钮:被点击了EventBus post")); finish(); } }); } }
5、接收消息
接收消息时,我们使用EventBus中最常用的onEventMainThread()函数来接收消息,具体为什么用这个,我们下篇再讲,这里先给大家一个初步认识,要先能把EventBus用起来先。在MainActivity中重写onEventMainThread(FirstEvent event),参数就是我们自己定义的类:在收到Event实例后,我们将其中携带的消息取出,一方面Toast出去,一方面传到TextView中;@Subscribe public void onEventMainThread(FirstEvent event){ String msg="onEventMainThread收到了消息:" + event.getMsg(); tv_notify.setText(msg); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); }
后话:
现在EventBus升级到了3.0.0了,相比2.4.0版本可以不用约定OnEvent方法开头了,我们为其可以添加注解:
@Subscribe public void onEventMainThread(FirstEvent event){ String msg="onEventMainThread收到了消息:" + event.getMsg(); tv_notify.setText(msg); Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories