Interprocess Communication between Activity and Service using Messenger on Android

MyService

public class MyService extends Service {

    private final int JOB1 = 1;
    private final int JOB1Response = 2;
    private final int JOB2 = 3;
    private final int JOB2Response = 4;
    Messenger messenger = new Messenger(new MyServiceHandler());

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


    class MyServiceHandler extends Handler {

        Message MSG;
        String message;
        Bundle bundle = new Bundle();

        @Override
        public void handleMessage(Message msg) {

            try {

                switch (msg.what) {
                    case JOB1:

                        message = "This is the first message from service";
                        MSG = Message.obtain(null, JOB1Response);
                        bundle.putString("responseMessage", message);
                        MSG.setData(bundle);
                        msg.replyTo.send(MSG);

                        break;

                    case JOB2:

                        message = "This is the second message from service";
                        MSG = Message.obtain(null, JOB2Response);
                        bundle.putString("responseMessage", message);
                        MSG.setData(bundle);
                        msg.replyTo.send(MSG);

                        break;

                    default:
                        super.handleMessage(msg);
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }
    }
}

MainActivity

public class MainActivity extends AppCompatActivity {

    private final int JOB1 = 1;
    private final int JOB1Response = 2;
    private final int JOB2 = 3;
    private final int JOB2Response = 4;
    Messenger messenger = null;
    boolean isBound = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this, MyService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }


    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            messenger = new Messenger(service);
            isBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            messenger = null;
            isBound = false;
        }
    };

    // JOB1
    private void sendMessage1() {
        try {
            if (isBound){
                  Message msg = Message.obtain(null, JOB1);
                  msg.replyTo = new Messenger(new MainActivityHandler());
                  messenger.send(msg);
            }
           
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    // JOB2
    private void sendMessage2() {
        try {
            if (isBound){
                  Message msg = Message.obtain(null, JOB2);
                  msg.replyTo = new Messenger(new MainActivityHandler());
                  messenger.send(msg);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void LogMessage1(String msg)
    {
        Log.d("MSG",msg);
    }

    @Override
    protected void onStop() {

        unbindService(serviceConnection);
        isBound = false;
        messenger = null;

        super.onStop();
    }

    class MainActivityHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {

            String message;

            switch (msg.what) {
                case JOB1Response:
                    message = msg.getData().getString("responseMessage");

                    // do something here

                    LogMessage1(message);

                    break;
                case JOB2Response:
                    message = msg.getData().getString("responseMessage");

                    // do something here
                    break;
                default:
                    super.handleMessage(msg);
            }


        }
    }
}

AndroidManifest.xml

<service android:name=".MyService" android:process=":remote"/>

References
https://www.youtube.com/watch?v=yAmnirP0d4U&t=0s&list=PLshdtb5UWjSp0879mLeCsDQN6L73XBZTk
https://developer.android.com/guide/components/bound-services