EventBus
Simple event bus for Android
Latest version
Usage
mvn:
<dependency>
<groupId>dev.nick</groupId>
<artifactId>eventbus</artifactId>
<version>0.2</version>
<type>pom</type>
</dependency>
gradle
compile 'dev.nick:eventbus:0.2'
Samples
Config
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
EventBus.create(this).setDebuggable(true);
}
}
Activity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getInstance().publishEmptyEvent(Constants.EVENT_FAB_CLICKED);
}
});
Intent intent = new Intent(this, MyService.class);
startService(intent);
EventBus.getInstance().subscribe(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getInstance().publishEmptyEvent(Constants.EVENT_ACTIVITY_FINISHED);
}
@CallInMainThread
@Events(Constants.EVENT_LOG)
public void handleLog(Event event) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(event.getData().getString("data"));
}
Service
@Events(Constants.EVENT_ACTIVITY_FINISHED)
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
log("Service started.");
EventBus.getInstance().subscribe(this);
}
@Events(Constants.EVENT_FAB_CLICKED)
public void handleFabClick() {
log("handleFabClick");
}
@Events(Constants.EVENT_FAB_CLICKED)
public void handleFabClickWithParam(Event event) {
log("handleFabClickWithParam:" + event);
}
@ReceiverMethod
@Events({Constants.EVENT_FAB_CLICKED, Constants.EVENT_ACTIVITY_FINISHED})
public void customName() {
log("customName");
}
@ReceiverMethod
public void customNameWithParam(Event event) {
log("customNameWithParam:" + event);
}
void log(Object msg) {
Thread calling = Thread.currentThread();
Log.d("EventBusSample", String.valueOf(msg) + ", calling in thread:" + calling.getName());
Bundle data = new Bundle();
data.putString("data", String.valueOf(msg));
Event event = new Event(Constants.EVENT_LOG, data);
EventBus.getInstance().publish(event);
}
}
Remote call
public class EventBusServiceSample extends EventBusService {
}
public class Binding {
IEventBus mBus;
public Binding(IEventBus bus) {
this.mBus = bus;
}
void bind() {
try {
mBus.subscribe(new IEventReceiver.Stub() {
@Override
public void onReceive(Event event) throws RemoteException {
Log.d("EventBus.Binding", "onReceive:" + event);
}
@Override
public int[] events() throws RemoteException {
return new int[]{Constants.EVENT_FAB_CLICKED};
}
});
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
Proguard
# EventBus
-keep class dev.nick.eventbus.** {*;}
-keep public interface dev.nick.eventbus.** {*;}
-keep public @interface dev.nick.eventbus.** {*;}