Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to send message from service to activity

I build an AsyncTask that create a service and now I want to sent from service to AsyncTask message. my code on the AsyncTask is:

  class ResponseHandler extends Handler {
  public void handleMessage(Message message) {
       // Toast.makeText(this, "message from service",Toast.LENGTH_SHORT).show();
 }

hope that it will handle the message from service correct my if I wrong.

and from service tried to do this

     Message message = Message.obtain(null, MyService.ADD_RESPONSE_HANDLER);
     message.replyTo = messenger;
      try {
             myService.send(message);
             catch (RemoteException e) {
             e.printStackTrace();
         }

but my errors are cannot find symbol in lines:

 MyService.ADD_RESPONSE_HANDLER
  message.replyTo = messenger;
  try {
  myService.send(message);

What do I need to add? Please give me a code that will do the work. thanks a lot.

like image 313
Vitaly Menchikovsky Avatar asked Jun 29 '12 09:06

Vitaly Menchikovsky


1 Answers

One way is using ResultReceiver. Here is my complete blog post which I had recently posted with an Example.

How to update Activity from Service using ResultReceiver

like image 68
Lalit Poptani Avatar answered Sep 23 '22 02:09

Lalit Poptani