Questa guida mostra come segnalare lo stato di una richiesta di lavoro eseguita in un servizio in background al componente che ha inviato la richiesta. Ciò consente, ad esempio, di segnalare lo stato della richiesta nella UI di un oggetto Activity
. Il modo consigliato per inviare e ricevere lo stato è utilizzare un LocalBroadcastManager
, che limita gli oggetti Intent
di trasmissione ai componenti della tua app.
Segnala lo stato da un JobIntentService
Per inviare lo stato di una richiesta di lavoro in un elemento
JobIntentService
ad altri componenti, devi prima creare un elemento Intent
che contenga lo stato
nei relativi dati estesi. Come opzione, puoi aggiungere un'azione e un URI dati a questo
Intent
.
Dopodiché invia il Intent
chiamando il numero
LocalBroadcastManager.sendBroadcast()
. Il campo Intent
viene inviato a qualsiasi componente dell'applicazione registrato per riceverlo.
Per ottenere un'istanza di LocalBroadcastManager
, chiama
getInstance()
.
Ecco alcuni esempi:
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); ... }
Il passaggio successivo consiste nel gestire gli oggetti Intent
di trasmissione in arrivo nel componente che ha inviato la richiesta di lavoro originale.
Ricevi trasmissioni di stato da un JobIntentService
Per ricevere oggetti Intent
di broadcast, utilizza una sottoclasse di
BroadcastReceiver
. Nella sottoclasse, implementa il metodo di callback BroadcastReceiver.onReceive()
, che LocalBroadcastManager
attiva quando riceve un Intent
. LocalBroadcastManager
passa il valore Intent
in arrivo a
BroadcastReceiver.onReceive()
.
Ecco alcuni esempi:
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. */ ... } }
Dopo aver definito BroadcastReceiver
, puoi definire i filtri corrispondenti
ad azioni, categorie e dati specifici. Per farlo, crea un
IntentFilter
. Questo primo snippet mostra come definire il 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"); ...
Per registrare BroadcastReceiver
e IntentFilter
nel sistema, ottieni un'istanza di LocalBroadcastManager
e chiama il relativo metodo registerReceiver()
. Il prossimo snippet mostra come registrare BroadcastReceiver
e i relativi 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); ...
Un singolo BroadcastReceiver
può gestire più di un tipo di oggetto
di trasmissione Intent
, ciascuno con la propria azione. Questa funzionalità consente di eseguire codice diverso per ogni azione, senza dover definire un BroadcastReceiver
separato per ogni azione. Per definire un altro
IntentFilter
per lo stesso
BroadcastReceiver
, crea il IntentFilter
e
ripeti la chiamata a
registerReceiver()
.
Ecco alcuni esempi:
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);
L'invio di una trasmissione Intent
non avvia o riprende una
Activity
. L'elemento BroadcastReceiver
per
Activity
riceve ed elabora oggetti Intent
anche
quando l'app è in background, ma non forza l'app a mettere in primo piano. Se vuoi
informare l'utente di un evento che si è verificato in background mentre la tua app non era
visibile, utilizza un Notification
. Non avviare mai una
Activity
in risposta a una trasmissione in arrivo
Intent
.