Android App后臺服務(wù)報告工作狀態(tài)實例
本節(jié)講運行在后臺服務(wù)里的工作請求,如何向發(fā)送請求者報告狀態(tài)。推薦用LocalBroadcastManager發(fā)送和接收狀態(tài),它限制了只有本app才能接收到廣播。
從IntentService匯報狀態(tài)
從IntentService發(fā)送工作請求狀態(tài)給其他組件,先創(chuàng)建一個包含狀態(tài)和數(shù)據(jù)的Intent。也可以添加action和URI到intent里。
下一步,調(diào)用 LocalBroadcastManager.sendBroadcast()發(fā)送Intent,應(yīng)用中所有注冊了接收該廣播的接收器都能收到。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的子類,實現(xiàn)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可以處理多種類型的廣播,這個特性允許你根據(jù)不同的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);
發(fā)送廣播并不會啟動或恢復(fù)Activity.BroadcastReceiver讓Activity能夠接收處理數(shù)據(jù),包括應(yīng)用在后臺的時候,但不會強制app回到前臺。如果你要在app在后臺,對用戶不可見時,通知用戶一個事件發(fā)生,用Notification。絕對不要啟動一個Activity來響應(yīng)廣播。
相關(guān)文章
Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變
這篇文章主要為大家詳細介紹了Android使用BroadcastReceiver監(jiān)聽網(wǎng)絡(luò)連接狀態(tài)的改變,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android實現(xiàn)recyclerview城市字母索引列表
大家好,本篇文章主要講的是Android實現(xiàn)recyclerview城市字母索引列表,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Android實現(xiàn)閃屏及注冊和登錄界面之間的切換效果
這篇文章主要介紹了Android實現(xiàn)閃屏及注冊和登錄界面之間的切換效果,實現(xiàn)思路是先分別實現(xiàn)閃屏、注冊界面、登錄界面的活動,再用Intent將相關(guān)的活動連接起來,實現(xiàn)不同活動之間的跳轉(zhuǎn),對android 實現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11快速解決安卓7.0系統(tǒng)寫入SD卡權(quán)限失敗的問題
今天小編就為大家分享一篇快速解決安卓7.0系統(tǒng)寫入SD卡權(quán)限失敗的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android ContentProvider實現(xiàn)獲取手機聯(lián)系人功能
這篇文章主要為大家詳細介紹了Android ContentProvider實現(xiàn)獲取手機聯(lián)系人功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07