package im.status.ethereum.module;

import android.app.Service;
import android.content.Intent;
import android.os.*;
import android.support.annotation.Nullable;
import android.util.Log;

import java.lang.ref.WeakReference;

public class StatusService extends Service {

    private static final String TAG = "StatusService";

    public StatusService() {
        super();
    }

    private static class IncomingHandler extends Handler {

        private final WeakReference<StatusService> service;

        IncomingHandler(StatusService service) {

            this.service = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message message) {

            StatusService service = this.service.get();
            if (service != null) {
                if (!service.handleMessage(message)) {
                    super.handleMessage(message);
                }
            }
        }
    }

    private final Messenger serviceMessenger = new Messenger(new IncomingHandler(this));

    private static Messenger applicationMessenger = null;

    private boolean handleMessage(Message message) {
        Log.d(TAG, "Received service message." + message.toString());
        applicationMessenger = message.replyTo;

        return true;
    }

    public static void signalEvent(String jsonEvent) {

        Log.d(TAG, "Signal event: " + jsonEvent);
        Bundle replyData = new Bundle();
        replyData.putString("event", jsonEvent);

        Message replyMessage = Message.obtain(null, 0, 0, 0, null);
        replyMessage.setData(replyData);
        sendReply(applicationMessenger, replyMessage);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return serviceMessenger.getBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "Status Service created!");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "Status Service stopped!");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }

    private static void sendReply(Messenger messenger, Message message) {
        try {
            boolean ex = false;
            if (messenger != null) {
                ex = true;
            }
            Log.d(TAG, "before sendReply " + ex);
            messenger.send(message);
        } catch (Exception e) {
            Log.e(TAG, "Exception sending message id: " + message.what, e);
        }
    }
}

@@ -177,16 +177,60 @@
 til.Log;
+%0Aimport java.util.concurrent.CountDownLatch;
 %0A%0Aimport
@@ -963,32 +963,118 @@
        %7D%0A    %7D%0A%0A
+    private static CountDownLatch applicationMessengerIsSet = new CountDownLatch(1);%0A%0A
     private fina
@@ -1376,16 +1376,56 @@
 replyTo;
+%0A%09applicationMessengerIsSet.countDown();
 %0A%0A      
@@ -1756,62 +1756,201 @@
 a);%0A
-        sendReply(applicationMessenger, replyMessage);
+%09try %7B%0A%09    applicationMessengerIsSet.await();%0A%09    sendReply(applicationMessenger, replyMessage);%0A%09%7D catch(InterruptedException e) %7B%0A%09    Log.d(TAG, %22Interrupted during event signalling.%22);%0A%09%7D
 %0A   

Add latch to ensure application messenger is set.