wangjiegulu / Rxandroideventssample
Licence: apache-2.0
BusEvents implementation base RxJava/RxAndroid
Stars: ✭ 85
Programming Languages
java
68154 projects - #9 most used programming language
RxAndroidEventsSample
BusEvents implementation base RxJava/RxAndroid/AndroidInject.
博客《基于RxJava、RxAndroid的EventBus实现》:
http://www.cnblogs.com/tiantianbyconan/p/4578699.html
Dependencies:
AndroidBucket:My base library AndroidInject:My Inject library RxJava RxAndroid retrolambda
How to use with Annotation(recommended)
It's realy simple to use with annotation
AndroidInject library)
1. You must be config presents to support annotations(For example: BaseActivity which extends AIAppCompatActivity in theprivate 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();
}
}
2. Write a method in your present(eg. your activity), and use an @Accept annotation
@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.
3. Post a event any where:
AddFeedsEvent addFeedsEvent = new AddFeedsEvent();
// Construct a AddFeedsEvent Object...
RxBus.get().post(addFeedsEvent);
4. Specify an Executor or Handler(Optional)
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;
}
});
}
}
How to use without Annotation(Not recommended)
Register an Observer
Observable<String> addOb = RxBus.get()
.register("addFeedTag", String.class);
addOb.observeOn(AndroidSchedulers.mainThread())
.subscribe(s -> {
// todo: Accept event and process here
});
Post a event any where
RxBus.get().post("addFeedTag", "hello RxBus!");
unregister the Observer
RxBus.get().unregister("addFeedTag", addOb);
License
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.
Note that the project description data, including the texts, logos, images, and/or trademarks,
for each open source project belongs to its rightful owner.
If you wish to add or remove any projects, please contact us at [email protected].