├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── example │ │ └── leon │ │ └── eventbusdemo │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── leon │ │ │ └── eventbusdemo │ │ │ ├── MyEvent.java │ │ │ ├── MyObject.java │ │ │ ├── MyService.java │ │ │ ├── MyStickyEvent.java │ │ │ ├── PublisherActivity.java │ │ │ ├── StickyActivity.java │ │ │ └── SubscriberActivity.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_publisher.xml │ │ └── activity_sticky.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── leon │ └── eventbusdemo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EventBus使用 2 | 3 | 开源地址:[https://github.com/greenrobot/EventBus](https://github.com/greenrobot/EventBus) 4 | 5 | 官方文档:[http://greenrobot.org/eventbus/documentation/](http://greenrobot.org/eventbus/documentation/) 6 | 7 | 8 | ## 使用步骤 9 | 10 | ### 1. 在Module的build.gradle添加依赖 11 | 12 | compile 'org.greenrobot:eventbus:3.0.0' 13 | 14 | 15 | ### 2. 创建事件 16 | public class MyEvent { 17 | public String msg; 18 | public MyEvent(String msg) { 19 | this.msg = msg; 20 | } 21 | } 22 | 23 | ### 3. 注册和反注册EventBus 24 | #### 在Activity中 #### 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_main); 29 | //注册事件总线 30 | EventBus.getDefault().register(this); 31 | } 32 | 33 | 34 | @Override 35 | protected void onDestroy() { 36 | super.onDestroy(); 37 | //反注册事件总线 38 | EventBus.getDefault().unregister(this); 39 | } 40 | 41 | #### 在Fragment中 #### 42 | 1. 在onCreate中注册,在onDestory中反注册 43 | 2. 在onCreateView中注册,在onDestoryView中反注册 44 | 45 | #### 在自定义控件中 #### 46 | 在onAttachedToWindow中注册,在onDetachedFromWindow中反注册 47 | 48 | #### 普通类 #### 49 | 在类中创建注册方法和反注册方法,在合适的时机调用 50 | 51 | ### 4. 监听事件 52 | 53 | /** 54 | * POSTING线程模型:在哪个线程发布事件,就在哪个线程执行onPostingEvent方法 55 | */ 56 | @Subscribe(threadMode = ThreadMode.POSTING) 57 | public void onPostingEvent(MyEvent event) { 58 | Log.d(TAG, "onPostingEvent: " + Thread.currentThread().getName()); 59 | } 60 | 61 | 62 | /** 63 | * MAIN线程模型:不管是哪个线程发布事件,都在主线程执行onMainEvent方法 64 | */ 65 | @Subscribe(threadMode = ThreadMode.MAIN) 66 | public void onMainEvent(MyEvent event) { 67 | Log.d(TAG, "onMainEvent: " + Thread.currentThread().getName()); 68 | } 69 | 70 | /** 71 | * BACKGROUND线程模型:事件如果是在子线程发布,onBackgroundEvent方法就在该子线程执行,事件如果是在 72 | * 主线程中发布,onBackgroundEvent方法就在EventBus内部的线程池中执行 73 | */ 74 | @Subscribe(threadMode = ThreadMode.BACKGROUND) 75 | public void onBackgroundEvent(MyEvent event) { 76 | Log.d(TAG, "onBackgroundEvent: " + Thread.currentThread().getName()); 77 | } 78 | 79 | /** 80 | * ASYNC线程模型:不管事件在哪个线程发布,onAsyncEvent方法都在EventBus内部的线程池中执行 81 | */ 82 | @Subscribe(threadMode = ThreadMode.ASYNC) 83 | public void onAsyncEvent(MyEvent event) { 84 | Log.d(TAG, "onAsyncEvent: " + Thread.currentThread().getName()); 85 | } 86 | 87 | ### 5. 发布事件 88 | /** 89 | * 在主线程中发布事件 90 | * @param view 91 | */ 92 | public void onPublishEventOnMainThread(View view) { 93 | MyEvent event = new MyEvent("msg from publisher main thread"); 94 | EventBus.getDefault().post(event); 95 | } 96 | 97 | /** 98 | * 在子线程中发送事件 99 | * @param view 100 | */ 101 | public void onPublishEventOnBGThread(View view) { 102 | new Thread(new Runnable() { 103 | @Override 104 | public void run() { 105 | MyEvent event = new MyEvent("msg from publisher bg thread"); 106 | EventBus.getDefault().post(event); 107 | } 108 | }).start(); 109 | } 110 | 111 | ## 优先级和事件取消 ## 112 | [Priorities and Even Cancellation](http://greenrobot.org/eventbus/documentation/priorities-and-event-cancellation/) 113 | 114 | ## 粘性事件 ## 115 | [Sticky Events](http://greenrobot.org/eventbus/documentation/configuration/sticky-events/) 116 | 117 | ## 订阅者索引 ## 118 | [Subscriber Index](http://greenrobot.org/eventbus/documentation/subscriber-index/) 119 | 120 | 121 | ## EventBus源码分析 ## 122 | 123 | ### 单例模式 ### 124 | 125 | //简单单例,当多线程时,还是会创建多个示例 126 | public static EventBus getDefault() { 127 | if (defaultInstance == null) { 128 | defaultInstance = new EventBus(); 129 | } 130 | return defaultInstance; 131 | } 132 | 133 | //加锁单例,每次调用都检查是否加锁 134 | public static synchronized EventBus getDefault() { 135 | if (defaultInstance == null) { 136 | defaultInstance = new EventBus(); 137 | } 138 | return defaultInstance; 139 | } 140 | 141 | //两个非空,一个加锁 142 | public static EventBus getDefault() { 143 | if (defaultInstance == null) { 144 | synchronized (EventBus.class) { 145 | if (defaultInstance == null) { 146 | defaultInstance = new EventBus(); 147 | } 148 | } 149 | } 150 | return defaultInstance; 151 | } 152 | 153 | 154 | ### 注册 ### 155 | 156 | public void register(Object subscriber) { 157 | Class subscriberClass = subscriber.getClass(); 158 | //找到订阅者中所有的订阅方法 159 | List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass); 160 | synchronized (this) { 161 | for (SubscriberMethod subscriberMethod : subscriberMethods) { 162 | //将订阅方法记录下来保存到对应事件的订阅列表中 163 | //Map, CopyOnWriteArrayList> subscriptionsByEventType 164 | subscribe(subscriber, subscriberMethod); 165 | } 166 | } 167 | } 168 | 169 | ### 发布事件 ### 170 | 171 | public void post(Object event) { 172 | PostingThreadState postingState = currentPostingThreadState.get(); 173 | List eventQueue = postingState.eventQueue; 174 | eventQueue.add(event);.//添加到事件队列 175 | 176 | if (!postingState.isPosting) { 177 | postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper(); 178 | postingState.isPosting = true; 179 | if (postingState.canceled) { 180 | throw new EventBusException("Internal error. Abort state was not reset"); 181 | } 182 | try { 183 | while (!eventQueue.isEmpty()) { 184 | //发布的单个事件 185 | postSingleEvent(eventQueue.remove(0), postingState); 186 | } 187 | } finally { 188 | postingState.isPosting = false; 189 | postingState.isMainThread = false; 190 | } 191 | } 192 | } 193 | 194 | 195 | private void postSingleEvent(Object event, PostingThreadState postingState) throws Error { 196 | Class eventClass = event.getClass(); 197 | boolean subscriptionFound = false; 198 | if (eventInheritance) { 199 | ........ 200 | } else { 201 | //发布对应事件类型的事件 202 | subscriptionFound = postSingleEventForEventType(event, postingState, eventClass); 203 | } 204 | ......... 205 | } 206 | 207 | private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) { 208 | CopyOnWriteArrayList subscriptions; 209 | synchronized (this) { 210 | subscriptions = subscriptionsByEventType.get(eventClass);//获取对应事件的订阅列表 211 | } 212 | if (subscriptions != null && !subscriptions.isEmpty()) { 213 | //遍历订阅列表 214 | for (Subscription subscription : subscriptions) { 215 | postingState.event = event; 216 | postingState.subscription = subscription; 217 | boolean aborted = false; 218 | try { 219 | //发布事件到一个订阅 220 | postToSubscription(subscription, event, postingState.isMainThread); 221 | aborted = postingState.canceled; 222 | } 223 | ........ 224 | } 225 | return true; 226 | } 227 | ...... 228 | } 229 | 230 | private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) { 231 | //判断订阅方法的线程模型 232 | switch (subscription.subscriberMethod.threadMode) { 233 | case POSTING: 234 | //POSTING线程模型下,直接反射调用订阅方法 235 | invokeSubscriber(subscription, event); 236 | break; 237 | case MAIN: 238 | //MAIN线程模型 239 | if (isMainThread) { 240 | //如果当前是主线程,则直接反射调用订阅方法 241 | invokeSubscriber(subscription, event); 242 | } else { 243 | //如果当前不是主线程,则使用绑定主线程Looper的Handler在主线程调用订阅方法 244 | mainThreadPoster.enqueue(subscription, event); 245 | } 246 | break; 247 | case BACKGROUND: 248 | //BACKGROUND线程模型 249 | if (isMainThread) { 250 | //如果当前是主线程,则在EventBus内部的线程池中执行 251 | backgroundPoster.enqueue(subscription, event); 252 | } else { 253 | //如果当前是子线程,则直接在该子线程反射调用订阅方法 254 | invokeSubscriber(subscription, event); 255 | } 256 | break; 257 | case ASYNC: 258 | //ASYNC线程模型 259 | //直接在EventBus的线程池中执行 260 | asyncPoster.enqueue(subscription, event); 261 | break; 262 | default: 263 | throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode); 264 | } 265 | } 266 | 267 | 268 | 269 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | 7 | defaultConfig { 8 | applicationId "com.example.leon.eventbusdemo" 9 | minSdkVersion 15 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:25.3.1' 26 | compile 'org.greenrobot:eventbus:3.0.0' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\software\AndroidStudio\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/example/leon/eventbusdemo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/MyEvent.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | /** 4 | * Created by Leon on 2016/9/5. 5 | */ 6 | public class MyEvent { 7 | 8 | public MyEvent(String msg) { 9 | this.msg = msg; 10 | } 11 | 12 | public String msg; 13 | } 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/MyObject.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.util.Log; 4 | 5 | import org.greenrobot.eventbus.EventBus; 6 | import org.greenrobot.eventbus.Subscribe; 7 | import org.greenrobot.eventbus.ThreadMode; 8 | 9 | /** 10 | * Created by Leon on 0013. 11 | */ 12 | 13 | public class MyObject { 14 | 15 | 16 | private static final String TAG = "MyObject"; 17 | 18 | 19 | public void registerEventBus() { 20 | EventBus.getDefault().register(this); 21 | } 22 | 23 | 24 | public void unRegisterEventBus() { 25 | EventBus.getDefault().unregister(this); 26 | } 27 | 28 | @Subscribe(threadMode = ThreadMode.POSTING) 29 | public void onPostingEvent(MyEvent myEvent){ 30 | Log.d(TAG, "onPostingEvent: " + myEvent.msg); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/MyService.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.app.Service; 4 | import android.content.Intent; 5 | import android.os.IBinder; 6 | import android.support.annotation.Nullable; 7 | import android.util.Log; 8 | 9 | import org.greenrobot.eventbus.EventBus; 10 | import org.greenrobot.eventbus.Subscribe; 11 | import org.greenrobot.eventbus.ThreadMode; 12 | 13 | /** 14 | * Created by Leon on 0013. 15 | */ 16 | 17 | public class MyService extends Service { 18 | 19 | private static final String TAG = "MyService"; 20 | 21 | @Override 22 | public void onCreate() { 23 | super.onCreate(); 24 | EventBus.getDefault().register(this); 25 | } 26 | 27 | @Nullable 28 | @Override 29 | public IBinder onBind(Intent intent) { 30 | return null; 31 | } 32 | 33 | /** 34 | * POSTING线程模型:在哪个线程发布事件,就在哪个线程执行onPostingEvent方法 35 | */ 36 | @Subscribe(threadMode = ThreadMode.POSTING, priority = 1) 37 | public void onPostingEvent(MyEvent event) { 38 | Log.d(TAG, "onPostingEvent:" + Thread.currentThread().getName()); 39 | } 40 | 41 | @Override 42 | public void onDestroy() { 43 | super.onDestroy(); 44 | EventBus.getDefault().unregister(this); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/MyStickyEvent.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | /** 4 | * Created by Leon on 0013. 5 | */ 6 | 7 | public class MyStickyEvent { 8 | } 9 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/PublisherActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.view.View; 8 | 9 | import org.greenrobot.eventbus.EventBus; 10 | 11 | /** 12 | * Created by Leon on 0013. 13 | */ 14 | 15 | public class PublisherActivity extends AppCompatActivity { 16 | 17 | private static final String TAG = "PublisherActivity"; 18 | 19 | @Override 20 | protected void onCreate(@Nullable Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_publisher); 23 | } 24 | 25 | /** 26 | * 在主线程中发布事件 27 | * @param view 28 | */ 29 | public void onPublishEventOnMainThread(View view) { 30 | Log.d(TAG, "onPublishEventOnMainThread: "); 31 | MyEvent event = new MyEvent("主线程的消息"); 32 | EventBus.getDefault().post(event); 33 | } 34 | 35 | /** 36 | * 在子线程中发送事件 37 | * @param view 38 | */ 39 | public void onPublishEventOnBGThread(View view) { 40 | Log.d(TAG, "onPublishEventOnBGThread: "); 41 | new Thread(new Runnable() { 42 | @Override 43 | public void run() { 44 | MyEvent event = new MyEvent("后台线程的消息"); 45 | EventBus.getDefault().post(event); 46 | } 47 | }).start(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/StickyActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | 8 | import org.greenrobot.eventbus.EventBus; 9 | import org.greenrobot.eventbus.Subscribe; 10 | import org.greenrobot.eventbus.ThreadMode; 11 | 12 | /** 13 | * Created by Leon on 0013. 14 | */ 15 | 16 | public class StickyActivity extends AppCompatActivity { 17 | 18 | private static final String TAG = "StickyActivity"; 19 | 20 | @Override 21 | protected void onCreate(@Nullable Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_sticky); 24 | EventBus.getDefault().register(this); 25 | } 26 | 27 | @Subscribe( sticky = true, threadMode = ThreadMode.POSTING) 28 | public void onPostingEvent(MyStickyEvent event) { 29 | Log.d(TAG, "onPostingEvent:" + Thread.currentThread().getName()); 30 | } 31 | 32 | @Override 33 | protected void onDestroy() { 34 | super.onDestroy(); 35 | EventBus.getDefault().unregister(this); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/leon/eventbusdemo/SubscriberActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.leon.eventbusdemo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.util.Log; 8 | import android.view.View; 9 | 10 | import org.greenrobot.eventbus.EventBus; 11 | import org.greenrobot.eventbus.Subscribe; 12 | import org.greenrobot.eventbus.ThreadMode; 13 | 14 | /** 15 | * Created by Leon on 2016/9/5. 16 | */ 17 | public class SubscriberActivity extends AppCompatActivity{ 18 | 19 | private static final String TAG = "SubscriberActivity"; 20 | private MyObject mObject; 21 | 22 | @Override 23 | protected void onCreate(@Nullable Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | //注册事件总线 27 | EventBus.getDefault().register(this); 28 | 29 | Intent intent = new Intent(this, MyService.class); 30 | startService(intent); 31 | 32 | mObject = new MyObject(); 33 | mObject.registerEventBus(); 34 | } 35 | 36 | /** 37 | * POSTING线程模型:在哪个线程发布事件,就在哪个线程执行onPostingEvent方法 38 | */ 39 | @Subscribe(threadMode = ThreadMode.POSTING) 40 | public void onPostingEvent(MyEvent event) { 41 | Log.d(TAG, "onPostingEvent:" + Thread.currentThread().getName()); 42 | } 43 | 44 | 45 | /** 46 | * MAIN线程模型:不管是哪个线程发布事件,都在主线程执行onMainEvent方法 47 | */ 48 | @Subscribe(threadMode = ThreadMode.MAIN) 49 | public void onMainEvent(MyEvent event) { 50 | Log.d(TAG, "onMainEvent: " + Thread.currentThread().getName()); 51 | } 52 | 53 | /** 54 | * BACKGROUND线程模型:事件如果是在子线程发布,onBackgroundEvent方法就在该子线程执行,事件如果是在 55 | * 主线程中发布,onBackgroundEvent方法就在EventBus内部的线程池中执行 56 | */ 57 | @Subscribe(threadMode = ThreadMode.BACKGROUND) 58 | public void onBackgroundEvent(MyEvent event) { 59 | Log.d(TAG, "onBackgroundEvent: " + Thread.currentThread().getName()); 60 | } 61 | 62 | /** 63 | * ASYNC线程模型:不管事件在哪个线程发布,onAsyncEvent方法都在EventBus内部的线程池中执行 64 | */ 65 | @Subscribe(threadMode = ThreadMode.ASYNC) 66 | public void onAsyncEvent(MyEvent event) { 67 | Log.d(TAG, "onAsyncEvent: " + Thread.currentThread().getName()); 68 | } 69 | 70 | 71 | @Override 72 | protected void onDestroy() { 73 | super.onDestroy(); 74 | //反注册事件总线 75 | EventBus.getDefault().unregister(this); 76 | 77 | mObject.unRegisterEventBus(); 78 | } 79 | 80 | public void onStartPublisherActivity(View view) { 81 | Intent intent = new Intent(this, PublisherActivity.class); 82 | startActivity(intent); 83 | } 84 | 85 | public void onPublishStickyEvent(View view) { 86 | EventBus.getDefault().postSticky(new MyStickyEvent()); 87 | } 88 | 89 | public void onStartStickyEvent(View view) { 90 | Intent intent = new Intent(this, StickyActivity.class); 91 | startActivity(intent); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 |