0

I have a code for send message from service to activity. But I only send message to the activity that create the service. If I change the activity and service still active i need recive the messages in new activity. how?

I use this code:

Activity

public static Handler messageHandler = new MessageHandler();

...


    public static class MessageHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            int state = message.arg1;
            switch (state) {
            case HIDE:
                progressBar.setVisibility(View.GONE);
                break;
            case SHOW:
                progressBar.setVisibility(View.VISIBLE);
                break;
            }
        }
    }

Then, I create service passing the handler.

Intent startService = new Intent(context, SERVICE.class)
startService.putExtra("MESSENGER", new Messenger(messageHandler));
context.startService(startService);

When change the activity I stop receiving messages. I can create a messageHandler class in any activity? thats work? Thanks!

Jose M.
  • 219
  • 1
  • 2
  • 11

1 Answers1

0

Take a look at the EventBus library:

Also read about Observer pattern.

Hope it helps!

Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96