Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android官方培訓課程:如何回傳IntentService的處理結果

編輯:關於 Android 程式設計

本節課程的主題是:如何把IntentService中的執行結果返回給請求方。官方推薦的一種做法是使用LocalBroadcastManager來實現,它能夠把廣播的Intent限制在App內部,避免數據外洩給其他應用。

發送IntentService的處理結果

要把IntentService的處理結果發送給其他組件,首先需要創建一個Intent對象,並把執行結果放進這個Intent中。

接著,把剛才創建好的Intent通過LocalBroadcastManager.sendBroadcast()發送出去。凡是對該Intent註冊了接收者的組件,都能收到這個結果。可以通過getInstance()獲取LocalBroadcastManager的實例。

示例如下:

 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 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。在其實現類中重寫onReceive()方法,當LocalBroadcastManager把相應的Intent廣播出來後,該方法便會被自動回調。

舉個例子:

 // Broadcast receiver for receiving status updates from the IntentService private class ResponseReceiver extends BroadcastReceiver { // Prevents instantiation private 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_ACTION IntentFilter mStatusIntentFilter = new IntentFilter( Constants.BROADCAST_ACTION); // Adds a data filter for the HTTP scheme mStatusIntentFilter.addDataScheme("http"); 

要把BroadcastReceiver和IntentFilter註冊到系統,需要先獲取LocalBroadcastManager的實例,再調用它的registerReceiver()方法。下面的代碼演示了這個過程:

 // Instantiates a new DownloadStateReceiver DownloadStateReceiver mDownloadStateReceiver = new DownloadStateReceiver(); // Registers the DownloadStateReceiver and its intent filters LocalBroadcastManager.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 filter LocalBroadcastManager.getInstance(getActivity()).registerReceiver( mDownloadStateReceiver, mIntentFilter);

值得强调的是,發送廣播Intent並不會啟動或恢復Activity。即使App處於掛起狀態(後台),也同樣能接收到Intent。如果App處於掛起狀態,而某個任務完成後需要通知用戶,那麼應當使用Notification來實現,絕不要為了響應接收到的Intent廣播而去啟動Activity。

原文地址:https://developer.android.com/training/run-background-service/report-status.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved