このガイドでは、バックグラウンド サービスで実行されている処理リクエストのステータスを報告する方法について説明します。
リクエストを送信したコンポーネントに
送信されますこれにより、たとえば、
Activity
オブジェクトの UI でリクエスト。Google Chat のメッセージ送信に
ステータスを受信するには、LocalBroadcastManager
を使用します。
ブロードキャスト Intent
オブジェクトを、独自のアプリ内のコンポーネントに制限します。
JobIntentService からステータスを報告する
処理リクエストのステータスを
JobIntentService
からその他
作成するには、まずIntent
構成を作成し、その中に
拡張データですオプションとして、このファイルにアクションとデータ URI を
Intent
。
次に、以下を呼び出して Intent
を送信します。
LocalBroadcastManager.sendBroadcast()
。これにより、Intent
が
コンポーネントがアプリに含まれている必要があります。
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()
メソッドを呼び出します。次のスニペットは、BroadcastReceiver
を登録する方法を示しています。
とその 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); ...
1 つの BroadcastReceiver
で複数のタイプのブロードキャストを処理できる
それぞれ独自のアクションを持つ Intent
オブジェクト。この機能を使用すると
個別のコードを定義することなく、アクションごとに異なるコードを実行できます。
アクションごとに BroadcastReceiver
。別のファイアウォール ルールを
IntentFilter
: 同じ
BroadcastReceiver
で、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
。