Guide to Guava’s EventBus in Java

Setup

EventBus eventBus = new EventBus();

Creating Listeners

public class EventListener {
 
    private static int eventsHandled;
 
    @Subscribe
    public void stringEvent(String event) {
        eventsHandled++;
    }
}

Registering Listeners

EventListener listener = new EventListener();
eventBus.register(listener);

Unregistering Listeners

eventBus.unregister(listener);

Posting Events

@Test
public void givenStringEvent_whenEventHandled_thenSuccess() {
    eventBus.post("String Event");
    assertEquals(1, listener.getEventsHandled());
}

Posting Custom Events

public class CustomEvent {
    private String action;
 
    // standard getters/setters and constructors
}
@Subscribe
public void someCustomEvent(CustomEvent customEvent) {
    eventsHandled++;
}
@Test
public void givenCustomEvent_whenEventHandled_thenSuccess() {
    CustomEvent customEvent = new CustomEvent("Custom Event");
    eventBus.post(customEvent);
 
    assertEquals(1, listener.getEventsHandled());
}

Handling an Unsubscribed Event

@Subscribe
public void handleDeadEvent(DeadEvent deadEvent) {
    eventsHandled++;
}

References
http://www.baeldung.com/guava-eventbus

Android Notifications

 public void createNotification(View view) {
        // Prepare intent which is triggered if the
        // notification is selected
        Intent intent = new Intent(this, NotificationReceiverActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

        // Build notification
        // Actions are just fake
        Notification noti = new Notification.Builder(this)
                .setContentTitle("New mail from " + "[email protected]")
                .setContentText("Subject")
                .setSmallIcon(R.drawable.icon)
                .setContentIntent(pIntent)
                .setTicker("Subject")
                .addAction(R.drawable.icon, "Call", pIntent)
                .addAction(R.drawable.icon, "More", pIntent)
                .addAction(R.drawable.icon, "And more", pIntent).build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        // hide the notification after its selected
        noti.flags |= Notification.FLAG_AUTO_CANCEL;

        notificationManager.notify(0, noti);

    }

or start foreground service

startForeground(101,notification);

References
http://www.vogella.com/tutorials/AndroidNotifications/article.html
https://questdot.com/android-foreground-service/

Keep the screen awake on Android

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
  }

or

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">
    ...
</RelativeLayout>

References
https://developer.android.com/training/scheduling/wakelock

Updating the UI from a Timer using Handler on Android

private Handler handler = new Handler();
handler.postDelayed(runnable, 100);
private Runnable runnable = new Runnable() {
   @Override
   public void run() {
      /* do what you need to do */
      foobar();
      /* and here comes the "trick" */
      handler.postDelayed(this, 100);
   }
};

If you want it to stop, you can just call handler.removeCallback(runnable) and it won’t start again, until you tell it to

References
http://www.mopri.de/2010/timertask-bad-do-it-the-android-way-use-a-handler/