96

I am trying some thing new on Android for which I need to access the handler of the UI thread.

I know the following:

  1. The UI thread has its own handler and looper
  2. Any message will be put into the message queue of the UI thread
  3. The looper picks up the event and passed it to the handler
  4. The handler handles the message and sends the specfic event to the UI

I want to have my service which has to get the UI thread handler and put a message into this handler. So that this message will be processed and will be issued to the UI. Here the service will be a normal service which will be started by some application.

I would like to know if this is possible. If so please suggest some code snippets, so that I can try it.

Regards Girish

iLikeAndroid
  • 1,106
  • 1
  • 8
  • 6

7 Answers7

187

This snippet of code constructs a Handler associated with the main (UI) thread:

Handler handler = new Handler(Looper.getMainLooper());

You can then post stuff for execution in the main (UI) thread like so:

handler.post(runnable_to_call_from_main_thread);

If the handler itself is created from the main (UI) thread the argument can be omitted for brevity:

Handler handler = new Handler();

The Android Dev Guide on processes and threads has more information.

volley
  • 6,651
  • 1
  • 27
  • 28
  • 3
    Tested it out and it works great! An example of a use case: I have a web interface which is being served by a server running directly on the device. Since the interface can be used to directly interact with the UI, and since the server needs to run on it's own thread, I needed a way to touch the UI thread from outside an Activity. The method you've described worked great. – mrPjer Jun 19 '12 at 20:53
  • perfect ^^ just used it to update my ui from a StreamingService. exactly what i needed thanks ! – An-droid Jan 15 '14 at 09:53
  • do you know if i can create a singleton instance of a handler, and use that every time i need to run something on the ui thread? – HelloWorld Feb 22 '18 at 12:36
  • I guess we'll never know – Denny Dec 11 '18 at 20:19
  • @HelloWorld yes that should work, assuming the Looper returned by Looper.getMainLooper() does not change. Did you try it? – volley Dec 12 '18 at 21:13
  • @volley I don't remember, but I don't think it's relevant to say if it worked or not. To be 100% sure it will always work, the Android documentation should state so explicitly, or one should check the source code to see if it can be deduced :) – HelloWorld Dec 13 '18 at 16:19
28

Create a Messenger object attached to your Handler and pass that Messenger to the Service (e.g., in an Intent extra for startService()). The Service can then send a Message to the Handler via the Messenger. Here is a sample application demonstrating this.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for this tip. This was helpful. Please see the following stack for a touch event flow to my activity MyDemo.dispatchTouchEvent(MotionEvent) line: 20 PhoneWindow$DecorView.dispatchTouchEvent(MotionEvent) line: 1696 ViewRoot.handleMessage(Message) line: 1658 ViewRoot(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 123 //Event handling starts here ActivityThread.main(String[]) line: 4203 Here the ViewRoot is a Handler. I want to get the reference of this handler...is it possible to get this from my application? – iLikeAndroid Jun 21 '11 at 06:04
  • @iLikeAndroid: If you did not create the `Handler`, you cannot access it, AFAIK. – CommonsWare Jun 21 '11 at 11:41
  • Thank you. I have tried to create an instance of ViewRoot. This is nothing but a handler. Now I am able to issue the messages on this handler. The handler is getting the message. But the ViewRoot is not able to process the message as it is not initialized properly. I need to call ViewRoot.setView() to initialize the proper data to ViewRoot. I want to know is there a default view or a base view etc, which I can use to initialise? – iLikeAndroid Jun 21 '11 at 14:50
  • @iLikeAndroid: There is no `ViewRoot` in the Android SDK, AFAICT. – CommonsWare Jun 22 '11 at 00:07
  • @CommonsWare - I know this is an old post, but what if the Activity only binds to the service (that's already launched, of course). How would you go about that? Would it be a good practice to send this same handler to the Activity that binds to the service? – hadez30 Oct 14 '15 at 12:53
  • 1
    @hadez30: Personally, I don't use bound services much. You're still welcome to use a `Handler`/`Messenger`, though I'd replace all that with an event bus (e.g., greenrobot's EventBus). – CommonsWare Oct 14 '15 at 12:56
  • I see. Thanks, CommonsWare. Was trying to avoid passing the handlers to Activities upon Activities. I guess that's the way to go. Will finally try using EventBus that I keep hearing about. – hadez30 Oct 14 '15 at 13:12
5

I suggest trying following code:

    new Handler(Looper.getMainLooper()).post(() -> {

        //UI THREAD CODE HERE



    });
bartop
  • 9,971
  • 1
  • 23
  • 54
user2983041
  • 1,761
  • 4
  • 19
  • 31
4

At the moment I prefer using event bus library such as Otto for this kind of problem. Just subscribe the desired components (activity):

protected void onResume() {
    super.onResume();
    bus.register(this);
}

Then provide a callback method:

public void onTimeLeftEvent(TimeLeftEvent ev) {
    // process event..
}

and then when your service execute a statement like this:

bus.post(new TimeLeftEvent(340));

That POJO will be passed to your above activity and all other subscribing components. Simple and elegant.

pram
  • 1,484
  • 14
  • 17
2

You can get values through broadcast receiver......as follows, First create your own IntentFilter as,

Intent intentFilter=new IntentFilter();
intentFilter.addAction("YOUR_INTENT_FILTER");

Then create inner class BroadcastReceiver as,

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    /** Receives the broadcast that has been fired */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction()=="YOUR_INTENT_FILTER"){
           //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
           String receivedValue=intent.getStringExtra("KEY");
        }
    }
};

Now Register your Broadcast receiver in onResume() as,

registerReceiver(broadcastReceiver, intentFilter);

And finally Unregister BroadcastReceiver in onDestroy() as,

unregisterReceiver(broadcastReceiver);

Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);

....cheers :)

Melbourne Lopes
  • 4,817
  • 2
  • 34
  • 36
1

In kotlin thats how you can do it

Let say if you want to show Toast message from service

val handler = Handler(Looper.getMainLooper())
handler.post {
   Toast.makeText(context, "This is my message",Toast.LENGTH_LONG).show()
}
Zohab Ali
  • 8,426
  • 4
  • 55
  • 63
0

Solution:

  1. Create a Handler with Looper from Main Thread : requestHandler
  2. Create a Handler with Looper from Main Thread: responseHandler and override handleMessage method
  3. post a Runnable task on requestHandler
  4. Inside Runnable task, call sendMessage on responseHandler
  5. This sendMessage result invocation of handleMessage in responseHandler.
  6. Get attributes from the Message and process it, update UI

Sample code:

    /* Handler from UI Thread to send request */

    Handler requestHandler = new Handler(Looper.getMainLooper());

     /* Handler from UI Thread to process messages */

    final Handler responseHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {

            /* Processing handleMessage */

            Toast.makeText(MainActivity.this,
                    "Runnable completed with result:"+(String)msg.obj,
                    Toast.LENGTH_LONG)
                    .show();
        }
    };

    for ( int i=0; i<10; i++) {
        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                   /* Send an Event to UI Thread through message. 
                      Add business logic and prepare message by 
                      replacing example code */

                    String text = "" + (++rId);
                    Message msg = new Message();

                    msg.obj = text.toString();
                    responseHandler.sendMessage(msg);
                    System.out.println(text.toString());

                } catch (Exception err) {
                    err.printStackTrace();
                }
            }
        };
        requestHandler.post(myRunnable);
    }
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211