This is a simple bus library based on android broadcast. We can use it very convenient.Messages that are sent and received are marked by class name, which is in line with our actual engineering application.
compile files('libs/broadcastbus-1.1.jar')
Copy the jar to the lib directory of your project and add the config to build.gradle.
BroadcastBus broadcastBus = new BroadcastBus(this);
The parameter must the context ,because the broadcastBus is based on Android Broadcast.
broadcastBus.register(UserInfoEvent.class, new OnEventReceive<UserInfoEvent>() {
@Override
public void onEvent(UserInfoEvent userInfoEvent) {
Log.d(TAG, "UserInfoEvent onEvent - received: " + userInfoEvent.toString());
}
});
Map<Class<?>, OnEventReceive> eventMap = new HashMap<>();
eventMap.put(UserInfoEvent.class, new OnEventReceive<UserInfoEvent>() {
@Override
public void onEvent(UserInfoEvent userInfoEvent) {
Log.d(TAG, "UserInfoEvent onEvent - received: " + userInfoEvent.toString());
}
});
//register a bus event
broadcastBus.register(eventMap);
Between,the UseInfoEvent class extends the BaseEvent class,and implement the Serializable interface.
broadcastBus.post(userInfoEvent);
Send the bus event and what registered this event will receive this event message.
broadcastBus.unRegister(LoginInfoEvent.class);
broadcastBus.unRegister();
You must unregister the bus event when is not used.
You have any questions to send email to me.Thanks.
- e-mail : [email protected]
- gmail : [email protected]