a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android App后臺服務報告工作狀態實例

2020-04-11 11:49:37
字體:
來源:轉載
供稿:網友

本節講運行在后臺服務里的工作請求,如何向發送請求者報告狀態。推薦用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來響應廣播。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日本视频在线 | 婷婷av在线| www国产一区 | 欧美亚洲国产日韩 | 在线免费观看的www视频 | 亚洲视频免费在线观看 | 黄av在线 | 狠狠操操 | 嫩呦国产一区二区三区av | 国产一级视频免费播放 | 久操成人 | 国产精品一二三区 | 欧美成人高清 | 中文字幕视频在线 | 男女看片黄全部免费 | www久久99| 一级黄色录像在线观看 | 99久久久国产精品免费蜜臀 | 成人精品视频在线观看 | 免费人成在线观看网站 | 天堂欧美城网站 | 婷婷激情综合 | 日本高清视频一区二区三区 | 蜜桃av一区| 欧美日韩国产综合视频 | 亚洲精品视频一区 | 青青久久 | 999视频网 | 久久福利 | 黄色高清网站 | 在线国产一区二区 | 欧美成人精品一区二区三区 | 欧美一区二区视频在线 | 一道本视频 | 妞干网福利视频 | 国产美女在线精品免费 | 国产成人av在线 | 色婷婷一区二区三区四区 | 天天精品 | 日韩欧美国产精品综合嫩v 久草久草久草 | 日韩欧美在线综合 |