Android App background services report working status instances

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

This section describes work requests running in the background service and how to report status to the sender. The LocalBroadcastManager send and receive status is recommended, which limits the ability to receive broadcasts only to this app.

Report status from IntentService

To send work request status from IntentService to other components, create an Intent with state and data. You can also add action and URI to intent.

In the next step, LocalBroadcastManager.sendBroadcast () is called to send Intent to all receivers in the application that are registered to receive the broadcast. LocalBroadcastManager.getInstance () gets an LocalBroadcastManager instance.


public final class Constants {
    ...
    // Defines a custom Intent action
    public static final String BROADCAST_ACTION =
        "com.example.android.threadsample.BROADCAST";
    ...
    // Defines the key for the status "extra" in an Intent
    public static final String EXTENDED_DATA_STATUS =
        "com.example.android.threadsample.STATUS";
    ...
}
public class RSSPullService extends IntentService {
...
    /*
     * Creates a new Intent containing a Uri object
     * BROADCAST_ACTION is a custom Intent action
     */
    Intent localIntent =
            new Intent(Constants.BROADCAST_ACTION)
            // Puts the status into the Intent
            .putExtra(Constants.EXTENDED_DATA_STATUS, status);
    // Broadcasts the Intent to receivers in this app.
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
...
}

The next step is to receive the broadcast and process it.

Receive broadcasts from IntentService

The receiving mode is similar to ordinary Broadcast1, which USES a subclass of BroadcastReceiver to implement the BroadcastReceiver.onReceive () method


// Broadcast receiver for receiving status updates from the IntentService
private class ResponseReceiver extends BroadcastReceiver
{
    // Prevents instantiation
    private DownloadStateReceiver() {
    }
    // Called when the BroadcastReceiver gets an Intent it's registered to receive
    @
    public void onReceive(Context context, Intent intent) {
...
        /*
         * Handle Intents here.
         */
...
    }
}

Once the sink class is defined, define the filter to match the specified action,categorie,data.

// Class that displays photos
public class DisplayActivity extends FragmentActivity {
    ...
    public void onCreate(Bundle stateBundle) {
        ...
        super.onCreate(stateBundle);
        ...
        // The filter's action is BROADCAST_ACTION
        IntentFilter mStatusIntentFilter = new IntentFilter(
                Constants.BROADCAST_ACTION);
 
        // Adds a data filter for the HTTP scheme
        mStatusIntentFilter.addDataScheme("http");
        ...

Registration is slightly different, LocalBroadcastManager.registerReceiver ().

  // Instantiates a new DownloadStateReceiver
        DownloadStateReceiver mDownloadStateReceiver =
                new DownloadStateReceiver();
        // Registers the DownloadStateReceiver and its intent filters
        LocalBroadcastManager.getInstance(this).registerReceiver(
                mDownloadStateReceiver,
                mStatusIntentFilter);
        ...

A single BroadcastReceiver can handle multiple types of broadcasts. This feature allows you to run different code for different action without having to define one BroadcastReceiver for each action.

  /*
         * Instantiates a new action filter.
         * No data filter is needed.
         */
        statusIntentFilter = new IntentFilter(Constants.ACTION_ZOOM_IMAGE);
        ...
        // Registers the receiver with the new filter
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
                mDownloadStateReceiver,
                mIntentFilter);

Sending the broadcast does not start or restore Activity.BroadcastReceiver to enable Activity to receive and process data, including when the application is in the background, but does not force app back to the foreground. If you want to notify the user of an event when app is in the background and not visible to the user, use Notification. Never start an Activity in response to a broadcast.


Related articles: