Getting Started
Installation
In Gradle
Add dependencies to your build.gradle
dependencies {
compile 'com.robocreative.android:robo-common:1.0'
compile 'com.robocreative.android:robo-messaging:1.0'
}
Defining Message
Every message has to be implementation of interface com.robo.messaging.Message. There is also an abstract class named com.robo.messaging.AbstractMessage providing the base for implementing Message. Below is an example:
package com.example.myapplication;
import com.robo.messaging.AbstractMessage;
public class MyMessage extends AbstractMessage<String> {
protected MyMessage(String content) {
super(content);
}
}
Creating Subscriber
Create a subscriber that receives messages of MyMessage
package com.example.myapplication;
import android.util.Log;
import com.robo.messaging.MessageBus;
import com.robo.messaging.Subscriber;
public class MySubscriber {
private MessageBus mMessageBus;
public MySubscriber(MessageBus messageBus) {
mMessageBus = messageBus;
}
public void doSubscribe() {
mMessageBus.subscribe(new Subscriber<MyMessage>() {
@Override
public void receive(MyMessage message) {
Log.d("MessageBus", "Received message: " + message.getContent());
}
});
}
}
Creating Publisher
Create a publisher that publishes a message of MyMessage
package com.example.myapplication;
import com.robo.messaging.MessageBus;
public class MyPublisher {
private MessageBus mMessageBus;
public MyPublisher(MessageBus messageBus) {
mMessageBus = messageBus;
}
public void doPublish(String content) {
mMessageBus.publish(new MyMessage(content));
}
}
Lauching the Test
Place following statements inside your application to launch the first test:
MessageBus messageBus = new MessageBusImp();
MySubscriber subscriber = new MySubscriber(messageBus);
subscriber.doSubscribe();
MyPublisher publisher = new MyPublisher(messageBus);
publisher.doPublish("Hello world!");
That's how to quickly use Message Bus in minutes. You can find more interesting features in upcoming pages.