3

I have a Service and i try to send message to my primary Activity just like this:

public void callAsynchronousTask() {
    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask doAsynchronousTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        Message m = new Message();
                        m.arg1 = 10;
                        m.arg2 = 15;
                        handler.sendMessage(m);
                        Log.i("Sent", "!");

                    } catch (Exception e) {}
                }
            });
        }

};
timer.schedule(doAsynchronousTask, 0, 3000); 
}

How can i fetch this message data in my Activity ?

Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

5 Answers5

2

In your activity, you should make a handler like this, and pass the reference to the handler to the service before you start it...

handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
    //msg.arg1
  }
};

but right now you are creating a handler inside the service but thats not what you wanted to do!

chuthan20
  • 5,389
  • 1
  • 17
  • 21
1

Another way would be to bind your Activity to your Service so that they can communicate.

Reference http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • My question is how to fetch data from a sent message – Adam Varhegyi Feb 22 '13 at 14:25
  • 1
    Moreso you should be trying to figure out a better way to have your Activity communicate with your service. – JoxTraex Feb 22 '13 at 14:28
  • 2
    Jox's approach is what you want here. Read about [Bound Services here](http://developer.android.com/guide/components/bound-services.html) - you'll see that you can provide a callback method which in your instance can expose a ````getArgs(Integer...)```` method or better. +1 – BrantApps Feb 22 '13 at 14:32
0

put your data in message.obj and take it from this field in your activity. Your data can be a class that you can define how you want it.

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
0

There is simple example project (which is created by CommonsWare) which shows how to send messages from Service to Activity via Handler. Check it out

Community
  • 1
  • 1
ashakirov
  • 12,112
  • 6
  • 40
  • 40
0
        //use this code to send data to activity

         Bundle data = new Bundle();
            data.putString("data", result);
            Message msg = Message.obtain();
            msg.setData(data);
            replyTo.sendMessage(msg);        //replyTo is handler declared in your main_Activity 






    //Pass this **replyTo** handler when you call your service from mainActivity..
          Handler replyTo= new Handler() {
            public void handleMessage(Message msg) {
            //get data here and do what you want..
        };
        };

Hope it helps..!

Gagan
  • 745
  • 10
  • 31