Otto
Otto is an Open Source project designed to provide an event bus implementation so that components can publish and subscribe to events.
PUBLISHING
Otto is an Open Source project designed to provide an event bus implementation so that components can publish and subscribe to events.
PUBLISHING
Event publishing is the most important part of the bus as it allows you to tell subscribers that an action has occurred. An instance of any class may be published on the bus and it will only be dispatched to subscribers for that type.
To publish a new event, call the post method:
BusProvider.getInstance().post(new TestData());
SUBSCRIBING
Subscription is the complement to event publishing—it lets you receive notification that an event has occurred. To subscribe to an event, annotate a method with @Subscribe. The method should take only a single parameter, the type of which will be the event you wish to subscribe to.
To listen for the event published in the previous section we would need the following:
@Subscribe public void showMessage(TestData msg)
{
Toast.makeText(getApplicationContext(),"Service Started",Toast.LENGTH_LONG).show();
}
PRODUCING
If new components, like a dynamically created fragment, should receive event data during their creation, components can register as producer for such event data with the @Produce annotation.
Event receivers must register via the register method of the Bus class.
@Produce
public String produceEvent() {
return "Starting up";
}
To Register and UnRegister
BusProvider.getInstance().register(MainActivity.this);
BusProvider.getInstance().unregister(MainActivity.this);
Uses
- Communicate between your activity and fragments
- Communicate between your activity and service
Otto Example
build.gradle
compile 'com.squareup:otto:1.3.8'
Singleton Class BusProvider
public final class BusProvider
{
private static Bus mInstance = null;
public BusProvider()
{
}
public static Bus getInstance(){
if(mInstance == null)
{
mInstance = new Bus();
}
return mInstance;
}
}
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.squareup.otto.Subscribe;
public class MainActivity extends AppCompatActivity {
Button registerButtton,unregisterButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
registerButtton=(Button)findViewById(R.id.register);
unregisterButton=(Button)findViewById(R.id.unregister);
registerButtton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BusProvider.getInstance().register(MainActivity.this);
}
});
unregisterButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BusProvider.getInstance().unregister(MainActivity.this);
}
catch (Exception e)
{
}
}
});
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
Intent serviceIntent=new Intent(MainActivity.this,MyService.class);
if(MyService.isRunning) {
stopService(serviceIntent);
}
startService(serviceIntent);
}
});
}
//To Subscribe event
@Subscribe public void showMessage(TestData msg)
{
Toast.makeText(getApplicationContext(),"Service Started",Toast.LENGTH_LONG).show();
}
}
Myservice.java
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service
{
public static boolean isRunning =false;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
isRunning =true;
}
@Override
public void onStart(Intent intent, int startid) {
//To Publish event
BusProvider.getInstance().post(new TestData());
}
@Override
public void onDestroy() {
}
}
TestData.java
public class TestData
{
public String msg;
}
Happy Coding :)
No comments:
Post a Comment