回報工作狀態

本指南說明如何將在背景服務中執行的工作要求狀態回報給傳送要求的元件。舉例來說,您可以利用這項功能 Activity 物件使用者介面中的要求。建議您使用 接收狀態是使用 LocalBroadcastManager, 限制將 Intent 物件廣播至您應用程式中的元件。

從 JobIntentService 回報狀態

如要在 訊息張貼時間:JobIntentService 請先建立包含狀態的 Intent 擴充資料。您可以選擇將動作和資料 URI 新增至這個 Intent

接著,請呼叫 Intent 來傳送 Intent LocalBroadcastManager.sendBroadcast()。這麼做會將 Intent 傳送給任何 註冊接收 SDK 的元件。 如要取得 LocalBroadcastManager 的例項,請呼叫 getInstance()

例如:

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

下一步是處理傳入的廣播 Intent 物件。 傳送原始工作要求的元件

接收 JobIntentService 的狀態廣播訊息

如要接收廣播 Intent 物件,請使用 BroadcastReceiver 的子類別。在子類別中,實作 BroadcastReceiver.onReceive() 回呼方法,LocalBroadcastManager 會在收到 Intent 時叫用此方法。LocalBroadcastManager 將傳入的 Intent 傳遞至 BroadcastReceiver.onReceive()

例如:

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

定義 BroadcastReceiver 後,您可以定義篩選器 以便與特定動作、類別和資料相符如要這麼做,請建立 IntentFilter。下面第一段程式碼片段說明如何定義篩選器:

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

如要註冊 BroadcastReceiver 和 系統支援 IntentFilter,並取得 LocalBroadcastManager,然後呼叫 registerReceiver() 方法。下一段程式碼片段說明如何註冊 BroadcastReceiverIntentFilter

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);
        ...

單一 BroadcastReceiver 可處理多種類型的廣播 Intent 物件,每個物件都有各自的動作。這項功能可讓您 就能為每個動作執行不同的程式碼,不必 每個動作BroadcastReceiver。如要為同一個 BroadcastReceiver 定義另一個 IntentFilter,請建立 IntentFilter,然後重複呼叫 registerReceiver()。例如:

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);

傳送廣播訊息 Intent 並未開始或繼續 Activity。的 BroadcastReceiver Activity 會接收及處理 Intent 物件 應用程式於背景運作,但不會強制應用程式在前景運作。如果發生以下情況: 想在應用程式未運作的情況下,通知使用者目前在背景發生的事件 顯示,請使用 Notification一律不啟動 Activity:回應傳入的廣播 Intent