emissary

Additional

Language
Java
Version
N/A
Created
Apr 6, 2015
Updated
Aug 20, 2018 (Retired)
Owner
Lucas Montano (lucasmontano)
Contributor
Lucas Montano (lucasmontano)
1
Activity
Badge
Generate
Download
Source code

Advertisement

Emissary

A simple way to bind and exchange messages between activities and services.

#What emissary do for you

  • Perform interprocess communication (IPC) using Messenger.
  • Implements the Handler, where the service receives the incoming Message and decides what to do.

#Activity Implementation

Declaring the emissary

    private Emissary.IEmissary emissary;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        emissary = Emissary.getInstance(this);
    }

Bind a service with emissary connection

  Intent intent = new Intent(this, WithLibService.class);
  bindService(intent, emissary.getServiceConnection(), Service.BIND_AUTO_CREATE);

Subscribe to a event

  emissary.subscribe(WithLibService.ON_CHANGE_TIME_ZONE, new Emissary.EmissaryMessengerCallback() {

      @Override
      public void data(Bundle data) {
          ((TextView) findViewById(R.id.time_zone)).setText(data.getString(WithLibService.ARG_TIME_ZONE));
      }
  });

Request something to service

  emissary.request(WithLibService.GET_TIME_ZONE, new Emissary.EmissaryMessengerCallback() {

    @Override
    public void data(Bundle data) {
        Toast.makeText(WithLibActivity.this, data.getString(WithLibService.ARG_TIME_ZONE), Toast.LENGTH_SHORT).show();
    }
  });

#Service Implementation

Declaring the Emissary

  private final Emissary.IEmissary emissary = Emissary.getInstance(this);

Binding

  @Override
  public IBinder onBind(Intent intent) {
      return emissary.getIBinder();
  }

Subscribe to respond activity's request

  public void onCreate() {
      super.onCreate();

      emissary.subscribe(GET_TIME_ZONE, new Emissary.EmissaryMessengerCallback() {
          @Override
          public void data(Bundle data) {
              String timeZone = TimeZone.getDefault().getDisplayName();
              Bundle bundle = new Bundle();
              bundle.putString(WithLibService.ARG_TIME_ZONE, timeZone);
              emissary.send(GET_TIME_ZONE, bundle);
          }
      });
  }

Send a Event to Subscribers

  String timeZone = TimeZone.getDefault().getDisplayName();
  Bundle bundle = new Bundle();
  bundle.putString(WithLibService.ARG_TIME_ZONE, timeZone);
  emissary.send(ON_CHANGE_TIME_ZONE, bundle);