Create a background service (original) (raw)

The [IntentService](/reference/android/app/IntentService) class provides a straightforward structure for running an operation on a single background thread. This allows it to handle long-running operations without affecting your user interface's responsiveness. Also, an[IntentService](/reference/android/app/IntentService) isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an [AsyncTask](/reference/android/os/AsyncTask)

An [IntentService](/reference/android/app/IntentService) has a few limitations:

However, in most cases an [IntentService](/reference/android/app/IntentService) is the preferred way to perform simple background operations.

This guide shows you how to do the following things:

Handle incoming intents

To create an [IntentService](/reference/android/app/IntentService) component for your app, define a class that extends [IntentService](/reference/android/app/IntentService), and within it, define a method that overrides [onHandleIntent()](/reference/android/app/IntentService#onHandleIntent%28android.content.Intent%29). For example:

Kotlin

class RSSPullService : IntentService(RSSPullService::class.simpleName)

override fun onHandleIntent(workIntent: Intent) {
    // Gets data from the incoming Intent
    val dataString = workIntent.dataString
    ...
    // Do work here, based on the contents of dataString
    ...
}

}

Java

public class RSSPullService extends IntentService { @Override protected void onHandleIntent(Intent workIntent) { // Gets data from the incoming Intent String dataString = workIntent.getDataString(); ... // Do work here, based on the contents of dataString ... } }

Notice that the other callbacks of a regular [Service](/reference/android/app/Service) component, such as[onStartCommand()](/reference/android/app/Service#onStartCommand%28android.content.Intent, int, int%29) are automatically invoked by[IntentService](/reference/android/app/IntentService). In an [IntentService](/reference/android/app/IntentService), you should avoid overriding these callbacks.

To learn more about creating an IntentService, see Extending the IntentService class.

Define the intent service in the manifest

An [IntentService](/reference/android/app/IntentService) also needs an entry in your application manifest. Provide this entry as a[<service>](/guide/topics/manifest/service-element) element that's a child of the[ <application>](/guide/topics/manifest/application-element) element:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name">
    ...
    <!--
        Because android:exported is set to "false",
        the service is only available to this app.
    -->
    <service
        android:name=".RSSPullService"
        android:exported="false"/>
    ...
</application>

The attribute android:name specifies the class name of the[IntentService](/reference/android/app/IntentService).

Notice that the[<service>](/guide/topics/manifest/service-element) element doesn't contain anintent filter. The[Activity](/reference/android/app/Activity) that sends work requests to the service uses an explicit [Intent](/reference/android/content/Intent), so no filter is needed. This also means that only components in the same app or other applications with the same user ID can access the service.

Now that you have the basic [IntentService](/reference/android/app/IntentService) class, you can send work requests to it with [Intent](/reference/android/content/Intent) objects. The procedure for constructing these objects and sending them to your [IntentService](/reference/android/app/IntentService) is described inSend work requests to the background service.