Android开源:Rxbus - 通过RxJava来替换EventBus
zhensf123456
8年前
<h2>RxBus</h2> <p>Event bus based on RxJava and optimized for Android.</p> <h2>Usage</h2> <p>We recommend obtaining the single instance of bus through injection or another appropriate mechanism.</p> <p>Or get singleton like following:</p> <pre> <code class="language-java">Bus bus = BusProvider.getInstance();</code></pre> <p>Subscribing</p> <p>To subscribe to an event, declare and annotate a method with @Subscribe. The method should be public and take only a single parameter.</p> <pre> <code class="language-java">@Subscribe public void onEvent(SomeEvent event) { // TODO: Do something }</code></pre> <p>You can also create subscription like following:</p> <pre> <code class="language-java">CustomSubscriber<SomeEvent> customSubscriber = bus.obtainSubscriber(SomeEvent.class, new Consumer<SomeEvent>() { @Override public void accept(SomeEvent someEvent) throws Exception { // TODO: Do something } }) .withFilter(new Predicate<SomeEvent>() { @Override public boolean test(SomeEvent someEvent) throws Exception { return "Specific message".equals(someEvent.message); } }) .withScheduler(Schedulers.trampoline());</code></pre> <p>Register and unregister your observer</p> <p>To receive events, a class instance needs to register with the bus.</p> <pre> <code class="language-java">bus.register(this);</code></pre> <p>The customSubscriber also needs to register with the bus.</p> <pre> <code class="language-java">bus.registerSubscriber(this, customSubscriber);</code></pre> <p>Remember to also call the unregister method when appropriate.</p> <pre> <code class="language-java">bus.unregister(this);</code></pre> <p>Publishing</p> <p>To publish a new event, call the post method:</p> <pre> <code class="language-java">bus.post(new SomeEvent("Message"));</code></pre> <p>Add RxBus to your project</p> <p>Gradle:</p> <pre> <code class="language-java">compile 'com.github.anadea:rxbus:1.0.1'</code></pre> <p>Maven:</p> <pre> <code class="language-java"><dependency> <groupId>com.github.anadea</groupId> <artifactId>rxbus</artifactId> <version>1.0.1</version> <type>pom</type> </dependency></code></pre> <h2>ProGuard</h2> <p>If you are using ProGuard, add the following lines to your ProGuard configuration file.</p> <pre> <code class="language-java">-keepattributes *Annotation* -keepclassmembers class ** { @com.anadeainc.rxbus.Subscribe public *; }</code></pre> <h2>License</h2> <pre> <code class="language-java">Copyright (C) 2017 Anadea Inc Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.</code></pre> <p> </p> <p> </p> <p> </p>