編輯:關於 Android 程式設計
本節課程的主題是:如何把IntentService中的執行結果返回給請求方。官方推薦的一種做法是使用LocalBroadcastManager來實現,它能夠把廣播的Intent限制在App內部,避免數據外洩給其他應用。
要把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對象了。
要接收廣播出來的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
使用 Android Studio 與 LeakCanary 檢測記憶體洩漏
記憶體洩漏為何難處理在 Android 開發中,記憶體洩漏常常不容易被察覺。它可能不會立即造成閃退,但隨著使用時間增加,應用會變得越來越卡頓,甚至觸發 OutOfMemo
Android 開發者入門指南(一):從零開始到開發環境建置
適合誰閱讀 這篇指南適合初次接觸 Java 或 Android 的讀者,尤其是希望系統性建立基礎、卻不知道從哪裡開始的人。若你已經具備部分基礎,也可以依照目錄快速補齊環境
把 Android Activity 變成彈出視窗樣式的做法
有時候我們希望某個 Activity 不要全屏,而是像一個浮動的小視窗,襯出底層畫面。這只要透過自訂主題就能辦到,核心是把 Activity 的主題設為 Dialog 風
Android 定位實戰:用 GPS 取得用戶位置並即時監聽位置變化
本文以實例講解 Android 如何透過 GPS 取得用戶的地理位置,並即時監聽位置的變化。以下內容供大家參考。首先是 LocationActivity.java 的完整