服務(wù)是什么?
Service是Android系統(tǒng)中的四大組件之一,主要有兩個(gè)應(yīng)用場景:后臺運(yùn)行和跨進(jìn)程訪問。
Service可以在后臺執(zhí)行長時(shí)間運(yùn)行操作而不提供用戶界面,除非系統(tǒng)必須回收內(nèi)存資源,否則系統(tǒng)不會(huì)停止或銷毀服務(wù)。服務(wù)可由其他應(yīng)用組件啟動(dòng),而且即使用戶切換到其他應(yīng)用,服務(wù)仍將在后臺繼續(xù)運(yùn)行。 此外,組件可以綁定到服務(wù),以與之進(jìn)行交互,甚至是執(zhí)行進(jìn)程間通信 (IPC)
值得注意的是:
- 服務(wù)并不是運(yùn)行在一個(gè)獨(dú)立的進(jìn)程中的,而是依賴于創(chuàng)建服務(wù)時(shí)所在的應(yīng)用程序進(jìn)程。當(dāng)某個(gè)應(yīng)用程序進(jìn)程被殺掉時(shí),所有依賴于該進(jìn)程的服務(wù)也會(huì)停止運(yùn)行。
- 服務(wù)并不會(huì)自動(dòng)開啟線程,所有代碼都是默認(rèn)運(yùn)行在主線程當(dāng)中的。我們需要在服務(wù)的內(nèi)部手動(dòng)創(chuàng)建子線程,并在這里執(zhí)行具體的任務(wù),否則就有可能造成主線程被阻塞的情況
注冊服務(wù)
服務(wù)Service作為四大組件,當(dāng)然也要在AndroidManifest.xml文件中注冊才能生效。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myservicetest">
<application
...
//注冊服務(wù)
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
定義一個(gè)服務(wù)
要?jiǎng)?chuàng)建服務(wù),必須創(chuàng)建Service的子類或使用它的一個(gè)現(xiàn)有子類。
右擊項(xiàng)目包名->New->Service->Service新建服務(wù)。
Exported屬性表示是否允許除了當(dāng)前程序之外的其他程序訪問這個(gè)服務(wù)
Enable屬性表示是否啟用這個(gè)服務(wù)
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// All clients have unbound with unbindService()
return mAllowRebind;
}
@Override
public void onRebind(Intent intent) {
// A client is binding to the service with bindService(),
// after onUnbind() has already been called
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
myService繼承自Service,其中onBind()方法是Service中唯一的一個(gè)抽象方法,必須在子類中實(shí)現(xiàn)。
重寫生命周期的回調(diào)方法
- onCreate()
何時(shí):首次創(chuàng)建服務(wù)時(shí)。
注意:若服務(wù)已在運(yùn)行,則不會(huì)調(diào)用此方法。 - onDestroy()
何時(shí):當(dāng)服務(wù)不再使用且將被銷毀時(shí)。
作用:清理所有資源,如線程、注冊的偵聽器、接收器等。
注意:這是服務(wù)接收的最后一個(gè)調(diào)用。 - int onStartCommand(Intent intent, int flags, int startId)
何時(shí):當(dāng)另一個(gè)組件調(diào)用startService()請求啟動(dòng)服務(wù)時(shí)。
參數(shù):
intent:startService()啟動(dòng)服務(wù)時(shí)傳入的Intent;
startId:唯一id標(biāo)識此次服務(wù)的啟動(dòng)請求。
返回值:描述系統(tǒng)應(yīng)該如何在服務(wù)終止的情況下繼續(xù)運(yùn)行服務(wù)。 - IBinder onBind(Intent it)
何時(shí):當(dāng)另一個(gè)組件調(diào)用bindService()與服務(wù)綁定時(shí)。
返回值:供客戶端與服務(wù)進(jìn)行通信。
啟動(dòng)和停止服務(wù):
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startService = (Button)findViewById(R.id.start_service);
Button stopService = (Button)findViewById(R.id.stop_service);
startService.setOnClickListener(this);
stopService.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);
break;
default:
break;
}
}
}
啟動(dòng)服務(wù):
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
停止服務(wù):
服務(wù)必須通過調(diào)用stopSelf()自行停止運(yùn)行,或者由另一個(gè)組件通過調(diào)用stopService()來停止它。
服務(wù)的銷毀
- 調(diào)用startService()啟動(dòng)服務(wù),則服務(wù)將一直運(yùn)行,直到其自身使用stopSelf()或由其他組件調(diào)用stopService()來停止。
- 調(diào)用bindService()創(chuàng)建并綁定服務(wù),則服務(wù)只會(huì)在該組件與其綁定時(shí)運(yùn)行。一旦該服務(wù)與所有客戶端之間的綁定全部取消,系統(tǒng)會(huì)銷毀它。
- 同時(shí)被啟動(dòng)和綁定的服務(wù),要經(jīng)歷上面兩種才能被銷毀。
- 僅當(dāng)內(nèi)存過低且必須回收系統(tǒng)資源以供具有用戶焦點(diǎn)的Activity使用時(shí),系統(tǒng)才會(huì)強(qiáng)制停止服務(wù)(前臺運(yùn)行的服務(wù)除外)。
活動(dòng)和服務(wù)進(jìn)行通信
上面Service基本用法中,啟動(dòng)Service之后,就可以在onCreate()或onStartCommand()方法里去執(zhí)行一些具體的邏輯了。不過這樣的話Service和Activity的關(guān)系并不大,只是Activity通知了Service一下:“你可以啟動(dòng)了。”然后Service就去忙自己的事情了。那么有沒有什么辦法能讓它們倆的關(guān)聯(lián)更多一些呢?比如說在Activity中可以指定讓Service去執(zhí)行什么任務(wù)。當(dāng)然可以,只需要讓Activity和Service建立關(guān)聯(lián)就好了。
觀察MyService中的代碼,你會(huì)發(fā)現(xiàn)一直有一個(gè)onBind()方法我們都沒有使用到,這個(gè)方法其實(shí)就是用于和Activity建立關(guān)聯(lián)的,修改MyService中的代碼,如下所示:
public class MyService extends Service {
//創(chuàng)建繼承自Binder類的DownloadBinder
class DownloadBinder extends Binder{
public void startDownload(){
Log.d("MyService","startDownload executed");
}
public int getProgress(){
Log.d("MyService","getProgress executed");
return 0;
}
}
//mBinder成員
private DownloadBinder mBinder = new DownloadBinder();
public MyService() {
}
//關(guān)鍵是這個(gè)onBind方法
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
...
}
然后在MainActivity中
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyService.DownloadBinder mDownloadBinder;
//創(chuàng)建一個(gè)ServiceConnection的匿名類
private ServiceConnection connection = new ServiceConnection() {
//重寫onServiceConnected方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mDownloadBinder = (MyService.DownloadBinder)service;
mDownloadBinder.startDownload();
mDownloadBinder.getProgress();
}
//重寫onServiceDisconnected方法
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startServiceBtn = (Button)findViewById(R.id.start_service);
Button stopServiceBtn = (Button)findViewById(R.id.stop_service);
startServiceBtn.setOnClickListener(this);
stopServiceBtn.setOnClickListener(this);
Button bindServiceBtn = (Button)findViewById(R.id.bind_service);
Button unbindServiceBtn = (Button) findViewById(R.id.unbind_service);
bindServiceBtn.setOnClickListener(this);
unbindServiceBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.start_service:
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);
break;
case R.id.stop_service:
Intent stopIntent = new Intent(this,MyService.class);
stopService(stopIntent);
break;
case R.id.bind_service:
Intent bindIntent = new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);//綁定服務(wù)
break;
case R.id.unbind_service:
unbindService(connection); //解綁服務(wù)
break;
default:
break;
}
}
}
綁定與解綁服務(wù)
流程簡述
-
先實(shí)現(xiàn)ServiceConnection
- 重寫onServiceConnected()
- 重寫onServiceDisconnected()
- 再調(diào)用bindService()綁定服務(wù)
與服務(wù)連接時(shí),系統(tǒng)會(huì)回調(diào)onServiceConnected,要保存IBinder對象,并使用其調(diào)用服務(wù)。
客戶端調(diào)用unbindService()解綁服務(wù)。
注意,此時(shí)不會(huì)回調(diào)onServiceDisconnected(),這個(gè)方法只會(huì)在服務(wù)crash或killed才會(huì)被回調(diào)。
何時(shí)綁定與何時(shí)解綁
- 若只需要在Activity可見時(shí)與服務(wù)交互,則應(yīng)在onStart()期間綁定,在onStop()期間解綁。
- 若希望Activity在后臺停止運(yùn)行時(shí)仍可接收響應(yīng),則在onCreate()期間綁定,在onDestroy()期間解綁。
- 切勿在onResume()期間綁定和onPause()期間解綁,這是因?yàn)槊恳淮紊芷谵D(zhuǎn)換都會(huì)發(fā)生這些回調(diào),頻率過高。
bindService()方法接收3個(gè)參數(shù)
參數(shù)1:構(gòu)建的Intent對象
參數(shù)2:ServiceConnection的實(shí)例
參數(shù)3:標(biāo)志位,傳入BIND_AUTO_CREATE表示在活動(dòng)和服務(wù)進(jìn)行綁定后自動(dòng)創(chuàng)建服務(wù)。
unbindService()方法接收一個(gè)參數(shù)
參數(shù)1:ServiceConnection的實(shí)例,用于解除活動(dòng)和服務(wù)之間的綁定。
任何一個(gè)服務(wù)在整個(gè)應(yīng)用程序范圍內(nèi)都是通用的,即MyService不僅可以和MainActivity綁定,還可以和任何其他的活動(dòng)進(jìn)行綁定,而且綁定后它們都可以獲取到相同的DownloadBinder實(shí)例。
服務(wù)的生命周期
服務(wù)的生命周期---從創(chuàng)建到銷毀---可以被分為以下兩個(gè)路徑:
- 啟動(dòng)類型的服務(wù):
一個(gè)組件調(diào)用startService()方法創(chuàng)建服務(wù),然后服務(wù)無限期的運(yùn)行,并且必須通過調(diào)用stopSelf()方法來終止自己。其他組件也能夠通過調(diào)用stopService()方法來終止這個(gè)服務(wù)。當(dāng)服務(wù)被終止,系統(tǒng)就會(huì)把它銷毀。 - 綁定類型的服務(wù):
一個(gè)組件(客戶端)調(diào)用bindService()方法創(chuàng)建服務(wù),客戶端通過IBinder接口與服務(wù)通信。客戶端能夠調(diào)用unbindService()方法來關(guān)閉與服務(wù)連接。多個(gè)客戶端能夠綁定到統(tǒng)一個(gè)服務(wù),并且當(dāng)所有的都解綁以后,系統(tǒng)就會(huì)銷毀這個(gè)服務(wù)。(服務(wù)不需要終止自己)
但是這兩個(gè)路徑不是完全獨(dú)立的。也就是說,你能夠綁定一個(gè)已經(jīng)用startService()方法啟動(dòng)的服務(wù)。
例如,一個(gè)后臺的音樂服務(wù)能夠調(diào)用帶有標(biāo)識要播放的音樂的Itent的startService()方法來啟動(dòng),稍后,可能在用戶想要進(jìn)行一些播放器的控制時(shí),或想要獲取有關(guān)當(dāng)前歌曲信息,那么一個(gè)Activity就能夠調(diào)用bindService()方法來綁定這個(gè)服務(wù)。在這個(gè)場景中,直到所有的客戶端解綁,stopService()或stopSelf()方法才能實(shí)際終止這個(gè)服務(wù)。
圖的左邊顯示了用startService()方法創(chuàng)建服務(wù)時(shí)的生命周期,圖的右邊顯示了用bindService()方法創(chuàng)建服務(wù)時(shí)的生命周期。
服務(wù)的更多技巧
使用前臺服務(wù):
服務(wù)的系統(tǒng)優(yōu)先級比較低,當(dāng)系統(tǒng)出現(xiàn)內(nèi)存不足的情況時(shí),就有可能會(huì)回收掉正在后臺運(yùn)行的服務(wù)。如果希望服務(wù)可以一直保持運(yùn)行狀態(tài),而不會(huì)由于系統(tǒng)內(nèi)存不足的原因?qū)е卤换厥眨涂梢钥紤]使用前臺服務(wù)。
前臺服務(wù)和普通服務(wù)的最大區(qū)別就在于:前臺服務(wù)會(huì)一直有一個(gè)正在運(yùn)行的圖標(biāo)在系統(tǒng)的狀態(tài)欄顯示,下拉狀態(tài)欄后可以看到更加詳細(xì)的信息,非常類似于通知的效果。
有些項(xiàng)目因?yàn)樘厥獾男枨髸?huì)要求必須使用前臺服務(wù),譬如彩云天氣預(yù)報(bào)應(yīng)用。
@Override
public void onCreate() {
super.onCreate();
Log.d("MyService","onCreate executed");
Intent intent = new Intent(this,MainActivity.class);
//PendingIntent傾向于在某個(gè)合適的時(shí)機(jī)去執(zhí)行某個(gè)動(dòng)作,簡單理解為延遲的Intent
PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
/* 第一個(gè)參數(shù)是通知的id,第二個(gè)參數(shù)是構(gòu)建出的Notification對象,
* 調(diào)用 startForeground()方法后就會(huì)讓MyService變成一個(gè)前臺服務(wù),并在系統(tǒng)狀態(tài)欄顯示出來
*/
startForeground(1,notification);
}
使用IntentService
為了簡單地創(chuàng)建一個(gè)異步的、會(huì)自動(dòng)停止的服務(wù),Android專門提供了一個(gè)IntentService類,這個(gè)類就很好地解決了 忘記開啟線程或者忘記調(diào)用stopSelf()方法的尷尬了。
新建一個(gè)繼承自IntentService的類
public class MyIntentService extends IntentService {
public MyIntentService(){
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
//打印當(dāng)前線程ID
Log.d("MyIntenteService","Thread id is " + Thread.currentThread().getId());
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("MyIntentService","onDestory executed");
}
}
在MainActivity中調(diào)用:
Intent intentService = new Intent(this,MyIntentService.class);
startService(intentService);
調(diào)用發(fā)現(xiàn)MyIntentService和MainActivity不僅所在的線程id不一樣,而且onDestory()方法也得到了執(zhí)行,說明MyIntentService在運(yùn)行完畢后確實(shí)自動(dòng)停止了。