Android創(chuàng)建服務(wù)之started service詳細(xì)介紹
創(chuàng)建started service
應(yīng)用組件(例如Activity)調(diào)用startService()來啟動一個Service,將需要的參數(shù)通過Intent傳給Service,Service將會在onStartCommand函數(shù)中獲得Intent。
有兩種方式可以創(chuàng)建started service,一種是擴(kuò)展Service類,另外一種是擴(kuò)展IntentService類
擴(kuò)展Service
這是所有服務(wù)的基類。擴(kuò)展這個類的時候,特別重要的一點(diǎn)是,需要創(chuàng)建一個新的線程來做服務(wù)任務(wù),因?yàn)閟ervice默認(rèn)是運(yùn)行在你的主線程(UI線程)中的,它會使你的主線程運(yùn)行緩慢。
擴(kuò)展IntentService
這是一個service的子類,他可以在一個工作線程中處理所有的啟動請求。如果你不需要同一時刻出來所有的服務(wù)請求,使用IntentService是一個很好的選擇。你需要做的僅僅是實(shí)現(xiàn)onHandlerIntent()方法,通過這個函數(shù)處理接受的每一個啟動請求。
下面我們學(xué)習(xí)如何擴(kuò)展IntentService類和Service類
擴(kuò)展IntentService類
IntentService做了什么?
1.創(chuàng)建一個主線程的工作線程,用于執(zhí)行通過onStartCommand()傳來的所有的intent。
2.創(chuàng)建一個工作隊(duì)列,將接受的所有intent一個一個的傳給onHandlerIntent(),所以同一時間內(nèi)你只處理一個intent,不用擔(dān)心多線程的問題。
3.當(dāng)所有的請求都處理完后,停止服務(wù),所以你不需要手動調(diào)用stopSelf()。
4.提供onBind()函數(shù)的默認(rèn)實(shí)現(xiàn),返回null
5.提供onStartCommand()函數(shù)的默認(rèn)實(shí)現(xiàn),它把intent發(fā)送到工作隊(duì)列中去,然后工作隊(duì)列再發(fā)送到你的onHandlerIntent()函數(shù)中。
有了上面的基礎(chǔ),你僅僅要做的就是實(shí)現(xiàn)onHandlerIntent()。并且實(shí)現(xiàn)一個小小的構(gòu)造函數(shù)。
參考下面的例子:
public class HelloIntentService extends IntentService {
/**
* A constructor is required, and must call the super IntentService(String)
* constructor with a name for the worker thread.
*/
public HelloIntentService() {
super("HelloIntentService");
}
/**
* The IntentService calls this method from the default worker thread with
* the intent that started the service. When this method returns, IntentService
* stops the service, as appropriate.
*/
@Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
}
}
如果你要實(shí)現(xiàn)其他的回調(diào)函數(shù),例如 onCreate , onStartCommand ,onDestroy 一定記得調(diào)用父類相應(yīng)的函數(shù),這樣IntentService才能正確的處理工作線程。
例如,你需要在onStartCommand函數(shù)中彈出一個提示,那么你可以這樣寫:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent,flags,startId);
}
有一點(diǎn)例外的是,如果你需要其他組件綁定服務(wù),那么你的onBind函數(shù)不需要調(diào)用父類的onBind。
在下一節(jié)中,你將會看到通過擴(kuò)展service類來實(shí)現(xiàn)與本節(jié)相同的服務(wù),所不同的是代碼更多,但是同樣意味著更靈活,特別是你需要同時處理多個請求時,比較適合直接擴(kuò)展Service。
擴(kuò)展Service類
如同你在上一節(jié)看到的一樣,使用IntentService來實(shí)現(xiàn)一個started service非常簡單。如果你需要你的service來執(zhí)行多個線程,那么你需要擴(kuò)展Service類去處理每個intent。
作為對比,下面的例子用Service類實(shí)現(xiàn)了和上一節(jié)中一樣功能的服務(wù)。
public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
long endTime = System.currentTimeMillis() + 5*1000;
while (System.currentTimeMillis() < endTime) {
synchronized (this) {
try {
wait(endTime - System.currentTimeMillis());
} catch (Exception e) {
}
}
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
如同你看到的,比IntentService的例子多了好多代碼。
因?yàn)槟阕约禾幚砻看蔚膐nStartcommand(),所以你可以在同一時間處理多個請求。這個例子沒有這樣做,但是如果你愿意,你完全可以每收到一個請求的時候,創(chuàng)建一個新線程并且立即運(yùn)行,而不是等到上一個請求處理完成后再運(yùn)行。
注意,onStartCommand()函數(shù)必須返回一個整數(shù)。這個整數(shù)描述了當(dāng)系統(tǒng)該服務(wù)時將要如何處理該服務(wù)。返回值必須是下面幾個值:
START_NOT_STICKY
在onStartCommand()返回后,系統(tǒng)服務(wù),服務(wù)將不會重啟,除非有pending intents(目前筆者還不懂什么是pending Intents)要投遞。這比較適合非常容易就可以重新啟動未完成任務(wù)的情況。
START_STICKY
在系統(tǒng)服務(wù)后,重啟服務(wù)并且調(diào)用onStartCommand(),但是不重新投遞上一次的intent。系統(tǒng)會傳給onStartCommand()函數(shù)null,除非有pending intent來啟動服務(wù),會傳給onStartCommand()相應(yīng)的intent。這種做法比較適合媒體播放服務(wù),不需要執(zhí)行命令,但是運(yùn)行,常處于等待任務(wù)的服務(wù)。
START_REDELIVER_INTENT
在系統(tǒng)服務(wù)后,重啟服務(wù)并且調(diào)用onStartCommand(),參數(shù)傳入上一次的intent,然后接下來是pending intent傳入onStartCommand()。這比較適合于正在執(zhí)行一項(xiàng)工作,它不能被立刻恢復(fù),例如下載文件。
啟動Service
你可以從一個Activity或者其他組件調(diào)用startService(intent)來啟動服務(wù)。Android系統(tǒng)會調(diào)用服務(wù)的onStartCommand()函數(shù)并且傳給它intent。
用上一節(jié)的HelloService來做個例子:
Intent intent = new Intent(this, HelloService.class);
startService(intent);
startService()會立刻返回,Android系統(tǒng)會調(diào)用服務(wù)的onStartCommand()函數(shù)。如果這是服務(wù)沒有在運(yùn)行,系統(tǒng)會首先調(diào)用onCreate()函數(shù),然后會緊接著調(diào)用onStartCommand()函數(shù)。
如果服務(wù)沒有提供綁定,那么通過startService()函數(shù)傳入的intent就是應(yīng)用組件和服務(wù)進(jìn)行交互的唯一途徑。如果你想服務(wù)發(fā)送結(jié)果給客戶組件,那么客戶組件需要在啟動服務(wù)時,創(chuàng)建一個用于廣播的PendingIntent,通過intent投遞給服務(wù)。這樣,服務(wù)就可以利用廣播把結(jié)果發(fā)送給客戶組件。
多個請求會導(dǎo)致服務(wù)的onStartCommand()被調(diào)用多次。但是,只需要一個停止請求(stopSelf() 或 stopService())就會使服務(wù)停止。
停止服務(wù)
一個啟動的服務(wù)必須管理自己的生命周期。因?yàn)槌窍到y(tǒng)需要回收資源的時候,系統(tǒng)不會停止或者銷毀服務(wù)。所以,服務(wù)必須通過調(diào)用stopSelf()來停止自己,或者有其他組件調(diào)用stopService()。
一擔(dān)收到stopSelf()或stopService()的停止請求,系統(tǒng)會立刻銷毀服務(wù)。
如果你的服務(wù)同時處理多個服務(wù)請求,當(dāng)某個請求完成時就不能馬上停止服務(wù),因?yàn)槟憧赡芤呀?jīng)又接受了一個新的請求(在第一個請求完成時結(jié)束服務(wù)會終止第二個請求)。為了解決這個問題,你可以調(diào)用stopSelf(int)函數(shù)來保證你的停止請求總是基于最近的一次服務(wù)請求。這是因?yàn)?,調(diào)用stopSelf(int)函數(shù)
會把onStartCommand()函數(shù)傳入的startId傳給停止請求。當(dāng)你的startId和最近一次接受的服務(wù)請求不匹配時,服務(wù)不會結(jié)束。
注意:stopSelf(int)函數(shù)只是簡單的與最近一次收到的startId比較,如果你的服務(wù)處理過程是多線程的,可能后收到的服務(wù)請求先完成,那stopSelf(int)的方案不適合你,你應(yīng)該手動管理接受到的服務(wù)請求和完成的服務(wù),比如在onStartCommand()函數(shù)中把startId記錄到一個表中,在完成服務(wù)任務(wù)時在表中記錄完成狀態(tài),在確保表中任務(wù)都完成的情況下直接調(diào)用stopSelf()來停止服務(wù)。
在前臺運(yùn)行服務(wù)
一個前臺服務(wù)是用戶能夠很明顯意識到的服務(wù),當(dāng)可用內(nèi)存比較低的時候,系統(tǒng)不會殺掉前臺服務(wù)。一個前臺服務(wù)必須提供一個狀態(tài)欄通知,放在正在運(yùn)行條目里,這意味著只要服務(wù)沒有停止或者一直是前臺服務(wù),這個通知就不會被消失。
舉個例子,一個音樂播放器服務(wù)應(yīng)該被設(shè)置為前臺運(yùn)行,因?yàn)橛脩舴浅C黠@知道他在運(yùn)行。這個狀態(tài)欄通知可以顯示正在播放的歌曲,并且可以允許用戶點(diǎn)擊進(jìn)入音樂播放界面。
要使服務(wù)運(yùn)行在前臺只要調(diào)用startForeground()。這個方法有兩個參數(shù):一個通知的整數(shù)ID和一個狀態(tài)欄通知。代碼片段:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, getText(R.string.notification_title),
getText(R.string.notification_message), pendingIntent);
startForeground(ONGOING_NOTIFICATION_ID, notification);
使用stopForeground()函數(shù)可以把服務(wù)從前臺移除。這個函數(shù)帶一個布爾值參數(shù),來表示是否從狀態(tài)欄移除通知。這個函數(shù)不會使服務(wù)停止。如果把前臺服務(wù)停止掉,那么通知也會同時被移除。
相關(guān)文章
總結(jié)Android中MD風(fēng)格相關(guān)控件
自Android5.0發(fā)布以來,谷歌推出全新的Material Desigen設(shè)計風(fēng)格,時過一年多了,在國內(nèi)也看到很多應(yīng)用在慢慢適應(yīng)MD設(shè)計風(fēng)格。今天小編給大家總結(jié)下Android中MD風(fēng)格相關(guān)控件的知識,有需要的可以參考學(xué)習(xí)。2016-08-08
詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫
本篇文章主要介紹了詳解Android數(shù)據(jù)存儲—使用SQLite數(shù)據(jù)庫,具有一定的參考價值,有興趣的可以了解一下。2017-03-03
Fragment跳轉(zhuǎn)時傳遞參數(shù)及結(jié)果回傳的方法(推薦)
今天總結(jié)一下Fragment間的參數(shù)傳遞及結(jié)果返回的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼
這篇文章主要介紹了Android單片機(jī)與藍(lán)牙模塊通信實(shí)例代碼,非常實(shí)用,特此分享給大家,需要的朋友可以參考下2016-05-05
Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼(附源碼)
這篇文章主要介紹了Android自定義多節(jié)點(diǎn)進(jìn)度條顯示的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-03-03
詳解Android TabHost的多種實(shí)現(xiàn)方法 附源碼下載
這篇文章主要為大家詳細(xì)介紹了Android TabHost的多種實(shí)現(xiàn)方法 文章中針對每一種實(shí)現(xiàn)方法都附有源碼進(jìn)行下載,感興趣的小伙伴們可以參考一下2016-05-05

