依赖于RxJava的编译时Android事件总线,并且支持Sticky(粘连)事件,以及多个Rx调度器.
我们需要引入一个apt插件到我们的classpath来开启注解处理功能.
buildscript { repositories { jcenter() } dependencies { //Android注解处理工具 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } allProjects { repositories { maven { url "https://www.jitpack.io" } } }
增加apt插件到项目的build.gradle配置文件中,使用apt插件来开启注解处理功能.
apply plugin: 'com.neenbedankt.android-apt' dependencies { apt "com.github.lsxiao.Apollo:processor:0.1.4-alpha.1" compile "com.github.lsxiao.Apollo:apollo:0.1.4-alpha.1" compile 'io.reactivex:rxandroid:1.2.1'//实际操作时请使用最新的rxandroid版本,这仅仅是一个示例. }
在Application中初始化Apollo.
public class App extends Application { @Override public void onCreate() { super.onCreate(); //注意!SubscriberBinderImplement 是由Apollo在编译时生成的代码. //因为Apollo是java库,所以无法依赖于Android库(RxAndroid). //所以你必须提供一个AndroidSchedulers.mainThread()调度器来初始化Apollo. Apollo.get().init(SubscriberBinderImplement.instance(), AndroidSchedulers.mainThread()); } }
你可以在基类中来绑定和解绑Apollo.
public abstract class BaseActivity extends AppCompatActivity { private SubscriptionBinder mBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(getLayoutId()); mBinder = Apollo.get().bind(this); afterCreate(savedInstanceState); } @Override protected void onDestroy() { super.onDestroy(); mBinder.unbind(); } protected abstract int getLayoutId(); protected abstract void afterCreate(Bundle savedInstanceState); }
在你喜欢的地方来接收事件.
默认使用
@Receive(tag = TAG) public void receiveEvent(Event event) { //do something. }
无参使用
@Receive(tag = TAG) public void showDialog(){ //show dialog. }
多个Tag
@Receive(tag = {TAG1,TAG2}) public void showDialog(){ //show dialog. }
只接受一次普通事件
//the event will be received only once. @Receive(tag = TAG,type = Receive.Type.NORMAL_ONCE) public void showDialog(){ //show dialog. }
调度器
//the subscribeOn and observeOn support main, io, new, computation, trampoline, immediate schedulers. //subscribeOn default scheduler is io. //observeOn default scheduler is main. @Receive(tag = TAG,subscribeOn = Receive.Thread.IO, observeOn = Receive.Thread.MAIN) public void receiveUser() { //do something. }
接收sticky事件
@Receive(tag = TAG,type = Receive.Type.STICKY) public void receiveEvent(Event event) { //do something. }
接收后清除该sticky事件
@Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE) public void receiveEvent(Event event) { //do something. }
接收后清除所有的sticky事件
@Receive(tag = TAG,type = Receive.Type.STICKY_REMOVE_ALL) public void receiveEvent(Event event) { //do something. }
//a normal event Apollo.get().send(EVENT_SHOW_USER, new User("lsxiao")); //a non-parameter event Apollo.get().send(EVENT_SHOW_USER); //a sticky event Apollo.get().sendSticky(EVENT_SHOW_BOOK, new Book("A Song of Ice and Fire"));
欢迎所有的 pull requests.
知乎 : @面条
Github : @lsxiao
Copyright 2016 lsxiao, 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.