# EventBus **Repository Path**: feishen98/EventBus ## Basic Information - **Project Name**: EventBus - **Description**: [EventBus] is a publish/subscribe event bus for openhormony and Java. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 3 - **Created**: 2021-06-16 - **Last Updated**: 2021-09-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README EventBus ======== [EventBus] is a publish/subscribe event bus for openhormony and Java.
EventBus... * simplifies the communication between components * decouples event senders and receivers * performs well with Activities, Fragments, and background threads * avoids complex and error-prone dependencies and life cycle issues * makes your code simpler * is fast * is tiny (~60k jar) * is proven in practice by apps with 1,000,000,000+ installs * has advanced features like delivery threads, subscriber priorities, etc. ------------ Add EventBus to your project ```gradle allprojects{ repositories{ mavenCentral() } } implementation 'io.openharmony.tpc.thirdlib:EventBus:1.0.4' annotationProcessor 'io.openharmony.tpc.thirdlib:eventbus-annotation-processor:1.0.1' ``` must add ``` tasks.withType(JavaCompile){ options.compilerArgs +=["-AeventBusIndex=org.greenrobot.eventbus.MyEventBusIndex"] } ``` ------------ If You need to take five steps to run the demo: ``` 1.Applying for a certificate is configured in build.gradle. 2.Delete the visible field from config.json in the demo. 3.add ohos.jar for module EventBus,the ohos.jar file path can be obtained from the SDK folder in ExternalLibraries. 4.you need at the EventBus library add libs file input "ohos.jar" 5.add "implementation files('libs/ohos.jar')" at the EventBus library build.gradle ``` -------------- EventBus in 4 steps ------------------- 1. initThreadForHos: ```java public class TestApplication extends AbilityPackage { @Override public void onInitialize() { super.onInitialize(); EventBus.initThreadForHos(getUITaskDispatcher()); } } ``` 2. Define events: ```java public static class MessageEvent { /* Additional fields if needed */ } ``` 3. Prepare subscribers: ```java @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) {/* Do something */}; ``` Register and unregister your subscriber. For example on openhormony, activities and fragments should usually register according to their life cycle: ```java @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { super.onStop(); EventBus.getDefault().unregister(this); } ``` 4. Post events: ```java EventBus.getDefault().post(new MessageEvent()); ``` not support ------------ 1. EVent-triggered ErrorDialog. R8, ProGuard ------------ If your project uses R8 or ProGuard add the following rules: ```bash -keepattributes *Annotation* -keepclassmembers class * { @org.greenrobot.eventbus.Subscribe ; } -keep enum org.greenrobot.eventbus.ThreadMode { *; } # And if you use AsyncExecutor: -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent { (java.lang.Throwable); } ``` License ------- Copyright (C) 2012-2020 Markus Junginger, greenrobot (https://greenrobot.org) EventBus binaries and source code can be used according to the [Apache License, Version 2.0](LICENSE).