針對(duì)Android四大組件之一的service做了一些細(xì)節(jié)的說明,本篇主要針對(duì)Android Service學(xué)習(xí)(一)作一些補(bǔ)充。
補(bǔ)充1
android的后臺(tái)指的是運(yùn)行是不依賴于UI界面的,即使activity被銷毀或者APP被關(guān)閉,只要進(jìn)程還在,service就可以繼續(xù)運(yùn)行,這一點(diǎn)依賴于service的線程是運(yùn)行在APP的進(jìn)程中的主UI線程。
Thread:用于開啟一個(gè)子線程去執(zhí)行一些比較耗時(shí)的操作,如下載等。之所以選擇在service中新建一個(gè)線程而不是在activity中建立,是因?yàn)閍ctivity很難對(duì)thread進(jìn)行控制,當(dāng)activity被銷毀,就沒有辦法重新獲得之前創(chuàng)建的線程的實(shí)例,此外,在一個(gè)A activity創(chuàng)建的子線程,在另一個(gè)B activity是無法對(duì)A創(chuàng)建的子線程進(jìn)行操作的。而所有的activity都可以與service進(jìn)行綁定,然后就可以操作其中的方法,即使activity被銷毀,之后只要重新與service進(jìn)行綁定,就可以重新獲取service中的Binder實(shí)例,不需要擔(dān)心無法對(duì)后臺(tái)控制的問題,activity也就可以放心的finish();
常用的service可以被寫成:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG,"onStartCommand excused");
new Thread(new Runnable() {
@Override
public void run() {
//開始執(zhí)行后臺(tái)任務(wù)
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
//具體的Binder實(shí)例
public class MyBinder extends Binder {
public void startDownLoad(){
Log.e(TAG,"開始下載");
new Thread(new Runnable() {
@Override
public void run() {
//執(zhí)行具體的耗時(shí)的下載任務(wù)
}
}).start();
}
}
補(bǔ)充2
針對(duì)service的開啟和停止,通過以下log來具體的展示:
1. 開啟
使用startService(intent)開啟服務(wù),調(diào)用log如下:
com.example.slide_table E/MyService:on Create excused
com.example.slide_table E/MyService:onStartCommand excused
com.example.slide_table E/MyService:onDestroy excused
使用bindService(intent)綁定服務(wù),調(diào)用log如下:
com.example.slide_table E/MyService: on Create excused
com.example.slide_table E/MyService: 開始下載
com.example.slide_table E/MyService: onDestroy excused
如上log的結(jié)果,在調(diào)用bind綁定服務(wù)的時(shí)候并不會(huì)執(zhí)行onStartCommand()函數(shù)。
2. 停止
使用startService(intent)開啟服務(wù),然后接著點(diǎn)擊bindService(intent),單獨(dú)的點(diǎn)擊stopService(intent)或者unbind(connection)都不會(huì)停止service,只有在點(diǎn)擊stopService(intent)后緊接著點(diǎn)擊unbind(connection),或者點(diǎn)擊unbind(connection)緊接著點(diǎn)擊stopService(intent)才會(huì)銷毀該service。
即:點(diǎn)擊stop按鈕只會(huì)讓service停止,點(diǎn)擊unbind按鈕只會(huì)讓service和activity解除綁定,一個(gè)service只有在既沒有和任何activity綁定的時(shí)候且又處于停止?fàn)顟B(tài)的時(shí)候才會(huì)被銷毀。
具體的log如下:
com.example.slide_table E/MyService: on Create excused//使用start開啟
com.example.slide_table E/MyService: onStartCommand excused
com.example.slide_table E/MyService: 開始下載//點(diǎn)擊bind按鈕
com.example.slide_table E/MyService: 停止線程//點(diǎn)擊stop按鈕
com.example.slide_table E/MyService: onDestroy excused//點(diǎn)擊unbind按鈕
com.example.slide_table E/MyService: on Create excused
com.example.slide_table E/MyService: onStartCommand excused
com.example.slide_table E/MyService: 開始下載
com.example.slide_table E/MyService: 解除綁定//點(diǎn)擊unbind按鈕
com.example.slide_table E/MyService: 停止線程//點(diǎn)擊stop按鈕
com.example.slide_table E/MyService: onDestroy excused
Android service的分享學(xué)習(xí)暫且告一段落,后續(xù)還會(huì)根據(jù)項(xiàng)目中實(shí)際遇到的service的應(yīng)用作說明,分析。如即將撰寫的關(guān)于Android藍(lán)牙開發(fā)中遇到的service的應(yīng)用。
以上博客中關(guān)于分享的知識(shí)點(diǎn)或者遇到的問題,以及不正確的地方還請(qǐng)幫忙指出,謝謝。。。