3

Basically, i have an activity that has a progress dialog, i'm sending a message to an Intent to load all the data from the internet without any hiccup in the application. However, i was able to send a message to the service but i wasn't able to resend the message to the activity. What to do?

Here's how i send message to the service:

final Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {

    }
};
final Intent intent = new Intent(this, FillingDatabase.class);
final Messenger messenger = new Messenger(handler);
intent.putExtra("messenger", messenger);
startService(intent);

And here's how i recieve the message from the service:

messenger = (Messenger) intent.getParcelableExtra("messenger");
        message = Message.obtain(null, 1234);
            messenger.send(message);

But i wasnt able to know how to recieve that message in an activity, can someone explain the approach? Thank you!

Fringo
  • 313
  • 5
  • 25

3 Answers3

2

Try out as below:

  private Handler handler = new Handler() 
{
    public void handleMessage(Message message) 
    {
       final Intent intent = new Intent(this, FillingDatabase.class);
       final Messenger messenger = new Messenger(handler);
       intent.putExtra("messenger", messenger);
       startService(intent);
    };
};
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

use

handler.dispatchMessage();

and send an empty message.declare handler static and access it by class name or simply create object of class and then access it.

i think you can also use

handler.SendEmptyMessage();

Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
0

Here comes your exact solution,

Issue was already solved here sending message from IntentService to Activity

And for more better understanding about this, Here comes an working example where you can directly use it for implementation Go here

Mark as OK if it works for you.

Community
  • 1
  • 1
sri2377076
  • 78
  • 2
  • 9