Android官方开发文档Training系列课程中文版:后台服务之响应IntentService的处理结果
原文地址:https://developer.android.com/training/run-background-service/report-status.html
這節課主要學習如何將IntentService中的執行結果返回給請求點。一種推薦的方式就是使用 LocalBroadcastManager來實現,它會將所廣播的Intent限制在APP內部。
發送IntentService的處理結果
為了可以將IntentService的處理結果發送給其它組件,首先需要創建一個Intent對象,并將執行結果放入該Intent內。
接下來要做的就是:將剛才創建好的Intent通過LocalBroadcastManager.sendBroadcast()發送出去。但凡是對該Intent注冊了的,那么發送該Intent都會收到結果。可以通過getInstance()獲取LocalBroadcastManager的實例。
例子如下:
public final class Constants {...// Defines a custom Intent actionpublic static final String BROADCAST_ACTION ="com.example.android.threadsample.BROADCAST";...// Defines the key for the status "extra" in an Intentpublic static final String EXTENDED_DATA_STATUS ="com.example.android.threadsample.STATUS";... } public class RSSPullService extends IntentService { .../** 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對象了。
接收IntentService的處理結果
如果需要接收廣播出來的Intent,那么就需要用到BroadcastReceiver了。在BroadcastReceiver的實現類中重寫onReceive()方法。當LocalBroadcastManager將相應的Intent對象廣播出來后,那么該方法就會被自動回調。
舉個例子:
// Broadcast receiver for receiving status updates from the IntentService private class ResponseReceiver extends BroadcastReceiver {// Prevents instantiationprivate DownloadStateReceiver() {}// Called when the BroadcastReceiver gets an Intent it's registered to receive@public void onReceive(Context context, Intent intent) { .../** Handle Intents here.*/ ...} }一旦定義好了BroadcastReceiver,那么就可以為其定義指定的意圖過濾器了。要做到這些,需要創建一個IntentFilter。下面的代碼演示了如何定義一個過濾器:
// Class that displays photos public class DisplayActivity extends FragmentActivity {...public void onCreate(Bundle stateBundle) {...super.onCreate(stateBundle);...// The filter's action is BROADCAST_ACTIONIntentFilter mStatusIntentFilter = new IntentFilter(Constants.BROADCAST_ACTION);// Adds a data filter for the HTTP schememStatusIntentFilter.addDataScheme("http");為了將BroadcastReceiver以及IntentFilter注冊到系統,需要先獲取LocalBroadcastManager的實例,然后再調用它的registerReceiver()方法。下面的代碼演示了這個過程:
// Instantiates a new DownloadStateReceiverDownloadStateReceiver mDownloadStateReceiver =new DownloadStateReceiver();// Registers the DownloadStateReceiver and its intent filtersLocalBroadcastManager.getInstance(this).registerReceiver(mDownloadStateReceiver,mStatusIntentFilter);...BroadcastReceiver可以同時處理多種類型的Intent對象,這項特性可以為每種Action定義不同的代碼,而不需要專門去定義BroadcastReceiver。為同一個BroadcastReceiver定義另外的IntentFilter,只需再創建一個IntentFilter,然后再次注冊一下就好:
/** Instantiates a new action filter.* No data filter is needed.*/statusIntentFilter = new IntentFilter(Constants.ACTION_ZOOM_IMAGE);...// Registers the receiver with the new filterLocalBroadcastManager.getInstance(getActivity()).registerReceiver(mDownloadStateReceiver,mIntentFilter);發送廣播Intent并不會啟動或者恢復Activity。就算是APP處于掛起狀態(處于后臺)也同樣會接收到Intent。如果APP處于掛起狀態的話,有任務完成需要通知到用戶,那么可以使用Notification做到。絕不要啟動Activity來響應接收到的Intent廣播。
總結
以上是生活随笔為你收集整理的Android官方开发文档Training系列课程中文版:后台服务之响应IntentService的处理结果的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hadoop背景、模块介绍、架构
- 下一篇: Android官方开发文档Trainin