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.

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

Complete source code is available here.

Android Basics – Learn how to create a Started Service using an IntentService

In the last Article you learnt about Android Services and the difference between a bound and a started service. In this Article you will learn how to call the IntentService you created earlier.

To create a started service you need to extend the IntentService class.

public class SampleService extends IntentService

Let’s take a simple example to illustrate how this works.

In the onHandleIntent method you will add a Log.

@Override
protected void onHandleIntent(Intent intent) {
    Log.v(TAG, "In onHandleIntent. Message will be printed after 10sec");
    if (intent != null) {
        synchronized (this){
            try {
                wait(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.v(TAG, "Service started");
        }
    }
}

To call this service you will add a button to the content_main.xml file

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_margin="15dp"
    android:id="@+id/callSampleServiceId"
    android:text="Start Service"
    android:background="@android:color/holo_blue_dark"
    android:textColor="@android:color/white"/>

On click of this button an explicit intent will be called.

if(v.getId() == R.id.callSampleServiceId){
    Log.v(TAG, "Service Button Clicked");

    Intent intent = new Intent(this, SampleService.class);
    startService(intent);
}

View the complete source code here.

Android Basics – Introduction to Services

A Service helps you to execute tasks in the background. Like downloading a file or tracking user location changes.

It is an Application Componenet with no user interface. It has a simple life cycle and comes with a set of features.

There are two types of Services

Bound Service  This kind of service is bound to a component like an Activity and runs as long as the Activity runs.

Started Service  This kind of service can run indefinitely. Even when the Activity that started is is destroyed.

You can create a new service by extending either the Service Class or the IntentService Class.

IntentService allows you to create a started service.

In the video you will learn how to get started by creating an IntentService.

Source Code is available here