Android USES IntentService to create a backend service instance

  • 2020-06-01 11:01:18
  • OfStack

IntentService provides a simple structure for running operations in a single background thread. This allows it to operate time-consuming operations without affecting the UI response. Also, IntentService does not affect UI lifecycle events, so it will continue to run in some cases where AsyncTask may be turned off (actually AsyncTask in onDestory of Activity will not run).

IntentService has the following limitations:

1. It cannot directly affect UI. To reflect the result to UI, you need to send it to Activity
2. Work requests are run sequentially. If an operation does not end, the operation sent later must wait for it to end (single thread)
3. Operations running in IntentService cannot be interrupted

However, in most cases, IntentService is the preferred method for simple background tasks.

This section shows how to create a subclass of IntentService, how to create an onHandleIntent() callback, and how to declare IntentService in AndroidManifest.xml.

Create IntentService

Define a subclass of IntentService and override the onHandleIntent() method:


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
        ...
    }
}

Tip: other normal Service callbacks, like onStartCommand(), are automatically called in IntentService. In IntentService, you should avoid overwriting these callbacks.

IntentService is defined in AndroidManifest.xml

IntentService is also Service), which needs to be registered in AndroidManifest.xml.


<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 android:name property specifies the class name of IntentService.

Note: & ltservice & The gt node cannot contain intent filter. The Activity that sends the work request USES an explicit Intent, specifying which IntentService. This also means that only a component in one app, or another application with the same user id, can access IntentService.

Now that you have the basic IntentService class, you can use the Intent object to send work requests.

Create and send work requests to IntentService

Create an explicit Intent, add the required data, and call startService() to send it to IntentService

/*
 * Creates a new Intent to start the RSSPullService
 * IntentService. Passes a URI in the
 * Intent's "data" field.
 */
mServiceIntent = new Intent(getActivity(), RSSPullService.class);
mServiceIntent.setData(Uri.parse(dataUrl));
//Call startService()
// Starts the IntentService
getActivity().startService(mServiceIntent);

Tip: work requests can be sent anywhere on Activity or Fragment. If you need to fetch user input first, you can send a work request in the click event or similar gesture callback method.

Once startService() is called,IntentService will work in onHandleIntent() and end itself.

The next step is to report the results to the original Activity or Fragment, which is implemented using BroadcastReceiver in the next section. Please refer to the article: https: / / www ofstack. com article / 51548. htm


Related articles: