page.title=Creating a Background Service trainingnavtop=true @jd:body
The {@link 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 {@link android.app.IntentService} isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an {@link android.os.AsyncTask}
An {@link android.app.IntentService} has a few limitations:
However, in most cases an {@link android.app.IntentService} is the preferred way to perform simple background operations.
This lesson shows you how to create your own subclass of {@link android.app.IntentService}. The lesson also shows you how to create the required callback method {@link android.app.IntentService#onHandleIntent onHandleIntent()}. Finally, the lesson describes shows you how to define the {@link android.app.IntentService} in your manifest file.
To create an {@link android.app.IntentService} component for your app, define a class that extends {@link android.app.IntentService}, and within it, define a method that overrides {@link android.app.IntentService#onHandleIntent onHandleIntent()}. For example:
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 {@link android.app.Service} component, such as {@link android.app.Service#onStartCommand onStartCommand()} are automatically invoked by {@link android.app.IntentService}. In an {@link android.app.IntentService}, you should avoid overriding these callbacks.
An {@link android.app.IntentService} also needs an entry in your application manifest.
Provide this entry as a
<service>
element that's a child of the
<application>
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
{@link android.app.IntentService}.
Notice that the
<service>
element doesn't contain an intent filter. The {@link android.app.Activity} that sends work
requests to the service uses an explicit {@link 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 {@link android.app.IntentService} class, you can send work requests to it with {@link android.content.Intent} objects. The procedure for constructing these objects and sending them to your {@link android.app.IntentService} is described in the next lesson.