本指南介绍如何报告在后台运行的工作请求的状态
向发送请求的组件传递数据例如,通过该功能,
在 Activity
对象的界面中发出请求。建议您通过
使用 LocalBroadcastManager
,
将广播 Intent
对象限制为您自己应用中的组件。
从 JobIntentService 报告状态
要在
JobIntentService
与其他
组件,请先创建一个 Intent
,并在其
扩展数据。作为一个选项,您可以向此
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); ...
单个 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
。