BusEvents implementation base RxJava/RxAndroid/AndroidInject.
博客《基于RxJava、RxAndroid的EventBus实现》:
http://www.cnblogs.com/tiantianbyconan/p/4578699.html
AndroidBucket:My base library
AndroidInject:My Inject library
RxJava
RxAndroid
retrolambda
It's realy simple to use with annotation
1. You must be config presents to support annotations(For example: BaseActivity which extends AIAppCompatActivity in the AndroidInject library)
private RxBusAnnotationManager rxBusAnnotationManager;
@Override
public void parserMethodAnnotations(Method method) throws Exception {
if (method.isAnnotationPresent(Accept.class)) {
if (null == rxBusAnnotationManager) {
rxBusAnnotationManager = new RxBusAnnotationManager(this);
}
rxBusAnnotationManager.parserObservableEventAnnotations(method);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != rxBusAnnotationManager) {
rxBusAnnotationManager.clear();
}
}
@Accept
public void onPostAccept(Object tag, AddFeedsEvent event) {
// todo: Accept event and process here (in main thread)
}
or
@Accept(
acceptScheduler = AcceptScheduler.NEW_THREAD,
value = {
@AcceptType(tag = ActionEvent.CLOSE, clazz = String.class),
@AcceptType(tag = ActionEvent.BACK, clazz = String.class),
@AcceptType(tag = ActionEvent.EDIT, clazz = String.class),
@AcceptType(tag = ActionEvent.REFRESH, clazz = String.class)
}
)
public void onPostAccept(Object tag, Object actionEvent) {
Logger.d(TAG, "[ActionEvent]onPostAccept action event name: " + actionEvent);
// todo: Accept event and process here (in new thread)
}
@Accept:
acceptScheduler: Perform this method on a specified scheduler;
value[]: AcceptType annotation array that specify the types this method can be accept.
@AcceptType:
tag: The tag of the event.
clazz: The Class of the event.
AddFeedsEvent addFeedsEvent = new AddFeedsEvent();
// Construct a AddFeedsEvent Object...
RxBus.get().post(addFeedsEvent);
public class MyApplication extends Application {
private Executor acceptExecutor = Executors.newCachedThreadPool();
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void onCreate() {
super.onCreate();
DefaultAcceptConfiguration.getInstance().registerAcceptConfiguration(new DefaultAcceptConfiguration.OnDefaultAcceptConfiguration() {
@Override
public Executor applyAcceptExecutor() {
return acceptExecutor;
}
@Override
public Handler applyAcceptHandler() {
return handler;
}
});
}
}
Observable<String> addOb = RxBus.get()
.register("addFeedTag", String.class);
addOb.observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> {
// todo: Accept event and process here
});
RxBus.get().post("addFeedTag", "hello RxBus!");
RxBus.get().unregister("addFeedTag", addOb);
Copyright 2015 Wang Jie
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.