Android App後台服務報告工作狀態執行個體_Android

來源:互聯網
上載者:User

本節講運行在後台服務裡的工作請求,如何向發送要求者報告狀態。推薦用LocalBroadcastManager發送和接收狀態,它限制了只有本app才能接收到廣播。

從IntentService彙報狀態

從IntentService發送工作請求狀態給其他組件,先建立一個包含狀態和資料的Intent。也可以添加action和URI到intent裡。

下一步,調用 LocalBroadcastManager.sendBroadcast()發送Intent,應用中所有註冊了接收該廣播的接收器都能收到。LocalBroadcastManager.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);
...
}

下一步是接收廣播並處理。

接收來自IntentService的廣播

接收方式與普通的Broadcast一樣,用一個BroadcastReceiver的子類,實現BroadcastReceiver.onReceive()方法

複製代碼 代碼如下:

// 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.
         */
...
    }
}

定義好了接收器類以後,定義過濾器,匹配指定的action,categorie,data.
複製代碼 代碼如下:

// 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");
        ...

註冊方式稍有不同,用LocalBroadcastManager.registerReceiver()。
複製代碼 代碼如下:

  // Instantiates a new DownloadStateReceiver
        DownloadStateReceiver mDownloadStateReceiver =
                new DownloadStateReceiver();
        // Registers the DownloadStateReceiver and its intent filters
        LocalBroadcastManager.getInstance(this).registerReceiver(
                mDownloadStateReceiver,
                mStatusIntentFilter);
        ...

單個BroadcastReceiver可以處理多種類型的廣播,這個特性允許你根據不同的action運行不同的代碼,而無需為每個action定義一個BroadcastReceiver。
複製代碼 代碼如下:

  /*
         * 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);

發送廣播並不會啟動或恢複Activity.BroadcastReceiver讓Activity能夠接收處理資料,包括應用在背景時候,但不會強制app回到前台。如果你要在app在後台,對使用者不可見時,通知使用者一個事件發生,用Notification。絕對不要啟動一個Activity來響應廣播。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.