Informar status do trabalho

Este guia mostra como relatar o status de uma solicitação de trabalho executada em um serviço em segundo plano para o componente que enviou a solicitação. Isso permite, por exemplo, informar o status da solicitação na interface de um objeto Activity. A maneira recomendada de enviar e receber status é usar um LocalBroadcastManager, que limita objetos Intent de transmissão a componentes no seu app.

Relatar o status a partir de um JobIntentService

Para enviar o status de uma solicitação de trabalho em um JobIntentService para outros componentes, primeiro crie um Intent que contenha o status nos dados estendidos. Como opção, você pode adicionar um URI de ação e dados a esse Intent.

Em seguida, envie Intent chamando LocalBroadcastManager.sendBroadcast(). Isso envia o Intent para qualquer componente no aplicativo que foi registrado para recebê-lo. Para acessar uma instância de LocalBroadcastManager, chame getInstance().

Por exemplo:

Kotlin

...
// Defines a custom Intent action
const val BROADCAST_ACTION = "com.example.android.threadsample.BROADCAST"
...
// Defines the key for the status "extra" in an Intent
const val EXTENDED_DATA_STATUS = "com.example.android.threadsample.STATUS"
...
class RSSPullService : JobIntentService() {
    ...
    /*
     * Creates a new Intent containing a Uri object
     * BROADCAST_ACTION is a custom Intent action
     */
    val localIntent = Intent(BROADCAST_ACTION).apply {
        // Puts the status into the Intent
        putExtra(EXTENDED_DATA_STATUS, status)
    }
    // Broadcasts the Intent to receivers in this app.
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent)
    ...
}

Java

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 JobIntentService {
...
    /*
     * 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);
...
}

A próxima etapa é processar os objetos Intent de transmissão de entrada no componente que enviou a solicitação de trabalho original.

Receber transmissões de status a partir de um JobIntentService

Para receber objetos Intent de transmissão, use uma subclasse de BroadcastReceiver. Na subclasse, implemente o método de callback BroadcastReceiver.onReceive(), que invoca LocalBroadcastManager quando recebe um Intent. LocalBroadcastManager transmite a Intent de entrada para BroadcastReceiver.onReceive().

Por exemplo:

Kotlin

// Broadcast receiver for receiving status updates from the IntentService.
private class DownloadStateReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        ...
        /*
         * Handle Intents here.
         */
        ...
    }
}

Java

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

Depois de definir o BroadcastReceiver, é possível definir filtros que correspondam a ações, categorias e dados específicos. Para fazer isso, crie uma IntentFilter. Este primeiro snippet mostra como definir o filtro:

Kotlin

// Class that displays photos
class DisplayActivity : FragmentActivity() {
    ...
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        super.onCreate(savedInstanceState)
        ...
        // The filter's action is BROADCAST_ACTION
        var statusIntentFilter = IntentFilter(BROADCAST_ACTION).apply {
            // Adds a data filter for the HTTP scheme
            addDataScheme("http")
        }
        ...

Java

// 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 statusIntentFilter = new IntentFilter(
                Constants.BROADCAST_ACTION);

        // Adds a data filter for the HTTP scheme
        statusIntentFilter.addDataScheme("http");
        ...

Para registrar o BroadcastReceiver e o IntentFilter no sistema, receba uma instância de LocalBroadcastManager e chame o método registerReceiver(). O próximo snippet mostra como registrar o BroadcastReceiver e o IntentFilter:

Kotlin

        // Instantiates a new DownloadStateReceiver
        val downloadStateReceiver = DownloadStateReceiver()
        // Registers the DownloadStateReceiver and its intent filters
        LocalBroadcastManager.getInstance(this)
                .registerReceiver(downloadStateReceiver, statusIntentFilter)
        ...

Java

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

Um único BroadcastReceiver pode processar mais de um tipo de objeto Intent de transmissão, cada um com a própria ação. Esse recurso permite que você execute um código diferente para cada ação, sem precisar definir um BroadcastReceiver separado para cada uma delas. Para definir outro IntentFilter para o mesmo BroadcastReceiver, crie a IntentFilter e repita a chamada para registerReceiver(). Por exemplo:

Kotlin

        /*
         * Instantiates a new action filter.
         * No data filter is needed.
         */
        statusIntentFilter = IntentFilter(ACTION_ZOOM_IMAGE)
        // Registers the receiver with the new filter
        LocalBroadcastManager.getInstance(this)
                .registerReceiver(downloadStateReceiver, statusIntentFilter)

Java

        /*
         * 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(this).registerReceiver(
                downloadStateReceiver,
                statusIntentFilter);

O envio de uma transmissão Intent não inicia nem retoma uma Activity. O BroadcastReceiver de um Activity recebe e processa objetos Intent mesmo quando o app está em segundo plano, mas não o força a ser executado em primeiro plano. Se você quiser notificar o usuário sobre um evento que ocorreu em segundo plano enquanto o app não estava visível, use um Notification. Nunca inicie uma Activity em resposta a uma Intent de transmissão recebida.