Melaporkan status pekerjaan

Panduan ini menunjukkan cara melaporkan status permintaan pekerjaan yang berjalan di layanan latar belakang ke komponen yang mengirim permintaan. Hal ini memungkinkan Anda, misalnya, melaporkan status permintaan di UI objek Activity. Cara yang disarankan untuk mengirim dan terima adalah menggunakan LocalBroadcastManager, yang membatasi objek Intent siaran ke komponen dalam aplikasi Anda sendiri.

Melaporkan status dari JobIntentService

Untuk mengirim status permintaan pekerjaan dalam JobIntentService ke komponen lain, buat terlebih dahulu Intent yang berisi status dalam data yang diperluas. Sebagai opsi, Anda dapat menambahkan URI data dan tindakan ke Intent ini.

Selanjutnya, kirim Intent dengan memanggil LocalBroadcastManager.sendBroadcast(). Tindakan ini mengirimkan Intent ke setiap komponen dalam aplikasi Anda yang telah didaftarkan untuk menerimanya. Untuk mendapatkan instance LocalBroadcastManager, panggil getInstance().

Contoh:

KotlinJava
...
// 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)
   
...
}
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);
...
}

Langkah berikutnya adalah menangani objek Intent siaran yang masuk komponen yang mengirim permintaan pekerjaan asli.

Menerima siaran status dari JobIntentService

Untuk menerima objek Intent siaran, gunakan subclass dari BroadcastReceiver. Di subclass, implementasikan metode Callback BroadcastReceiver.onReceive() , yang dipanggil LocalBroadcastManager saat menerima Intent. LocalBroadcastManager meneruskan Intent yang masuk ke BroadcastReceiver.onReceive().

Contoh:

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

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

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

...
   
}
}

Setelah menentukan BroadcastReceiver, Anda dapat menentukan filter yang cocok dengan tindakan, kategori, dan data tertentu. Untuk melakukannya, buat IntentFilter. Cuplikan pertama ini menunjukkan cara menentukan filter:

KotlinJava
// 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")
       
}
       
...
// 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");
       
...

Untuk mendaftarkan BroadcastReceiver dan IntentFilter dengan sistem, dapatkan instance LocalBroadcastManager, lalu panggil registerReceiver() . Cuplikan berikutnya ini menunjukkan cara mendaftarkan BroadcastReceiver dan IntentFilter-nya:

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

Satu BroadcastReceiver dapat menangani lebih dari satu jenis siaran Intent, masing-masing dengan tindakannya sendiri. Dengan fitur ini, Anda dapat menjalankan kode yang berbeda untuk setiap tindakan, tanpa harus mendefinisikan BroadcastReceiver untuk setiap tindakan. Untuk mendefinisikan IntentFilter untuk yang sama BroadcastReceiver, buat IntentFilter dan ulangi panggilan ke registerReceiver(). Contoh:

KotlinJava
        /*
         * 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)
        /*
         * 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
);

Mengirim siaran Intent tidak memulai atau melanjutkan Activity. BroadcastReceiver untuk Activity menerima dan memproses objek Intent, bahkan saat aplikasi berada di latar belakang, tetapi tidak memaksa aplikasi ke latar depan. Jika Anda ingin memberi tahu pengguna tentang peristiwa yang terjadi di latar belakang saat aplikasi Anda tidak terlihat, gunakan Notification. Jangan pernah memulai Activity sebagai respons terhadap siaran masuk Intent.