이 가이드에서는 백그라운드 서비스에서 실행된 작업 요청의 상태를 보고하는 방법을 보여줍니다.
요청을 전송한 구성요소로 전달됩니다. 예를 들어 이 기능을 사용하면
Activity 객체의 UI에서 요청을 처리합니다. 이러한 환경에서 데이터를
수신 상태는 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) ... }
자바
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. */ ... } }
자바
// 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") } ...
자바
// 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) ...
자바
// Instantiates a new DownloadStateReceiver DownloadStateReceiver downloadStateReceiver = new DownloadStateReceiver(); // Registers the DownloadStateReceiver and its intent filters LocalBroadcastManager.getInstance(this).registerReceiver( downloadStateReceiver, statusIntentFilter); ...
단일 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)
자바
/* * 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입니다.