Android Basics : Learn how to use a Handler with IntentService to update the Main Thread

In the last Article you saw how to call an IntentService. Then you printed a Log statement in the background thread, once the service completed.

If you want to display a message in the Main Thread then you can use a Handler. A Handler allows you to access the Main Thread once the service is complete.

Here is the sample source code to achieve this.

    protected void onHandleIntent(Intent intent) {
        Log.v(TAG, "In onHandleIntent. Message will be printed after 10sec");
        if (intent != null) {
            synchronized (this){
                try {
                } catch (InterruptedException e) {
                Log.v(TAG, "Service started");
       Runnable() {
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Service Completed" , Toast.LENGTH_SHORT).show();

Complete source code is available here.

Author: Ankur

I am Enthusiastic about Learning new things and sharing my Knowledge. I like programming and have a pretty good background in Computer Science.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: