4

I've been looking for quite some time for some good documentation or a good example of this. I need to make changes to my main activity UI from the worker thread in my service which is running in the background. As far as I understand I know that I am suppose to work with some sort of Handler but I am not sure exactly how to approach this.

Does anyone have any ideas or good examples that they could direct me to? The UI element I am changing is a TextView that is simply informing the user of the status of the thread.

Thanks for your help.

gtdevel
  • 1,513
  • 6
  • 21
  • 38

4 Answers4

7

All you have to do is to create a Handler on the UI thread:

private Handler serviceHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        someFunctionInTheUIThread();
    }
};

And then pass this through to your service. You could have a function in the Service like this:

public void registerHandler(Handler serviceHandler) {
    handler = serviceHandler;
}

and then pass the handler through like this:

theService = ((LocalBinder) service).getService();
theService.registerHandler(serviceHandler);

then to send a message back:

Message msg = handler.obtainMessage(IDENTIFIER, "Message or data");
handler.sendMessage(msg);
Martyn
  • 16,432
  • 24
  • 71
  • 104
  • Would it matter if I send the message from the thread in the service or from the onStartCommand? I am planning from sending it from both. – gtdevel Sep 02 '11 at 15:59
  • The beauty of Handlers is that they work across threads - read up here http://developer.android.com/reference/android/os/Handler.html. "A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue." – Martyn Sep 02 '11 at 16:01
  • So if I have a worker thread in my service, it's not running in the same thread as the service, is it? Does that mean I have to make two handler's to send messages from the service methods and then from the thread? Sorry if these are stupid questions but threading confuses me. – gtdevel Sep 02 '11 at 16:05
  • No. Create the `Handler` on the first thread (prob your UI thread) and then pass the handler all the way through to the worker thread in your service. It should just work :) – Martyn Sep 02 '11 at 16:08
  • What is the LocalBinder for. Do I need to write some code in my service before i use: theService = ((LocalBinder) service).getService(); theService.registerHandler(serviceHandler); – gtdevel Sep 02 '11 at 17:35
  • Have a look at the example on this page : http://developer.android.com/reference/android/app/Service.html – Martyn Sep 05 '11 at 08:42
2

Look into Service Binding. Or you could use BroadcastReceiver in your main activity to receive broadcasts from Service.

1

You have to send an intent from your service with sendBroadcast(intent) and set a BroadcastReceiver in your activity

crbin1
  • 2,219
  • 3
  • 22
  • 29
  • I tried that but I'm having some problems with it. I'll try the handler and then see if I can make the broadcast receiver work... Thanks. – gtdevel Sep 02 '11 at 15:56
1

Create a handler in onCreate() method in your main activity. This will create a handler in the UI thread. Then using this handler from the worker thread, call whatever you need to to get the TextView updated.

NujnaH
  • 173
  • 1
  • 8