Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a message from an Intent Service to an activity [duplicate]

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!

like image 238
Fringo Avatar asked Nov 16 '25 22:11

Fringo


1 Answers

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);
    };
};
like image 138
GrIsHu Avatar answered Nov 19 '25 12:11

GrIsHu