Android 服務(wù)(Service)適合執(zhí)行那些不需要和用戶交互而且還要求長(zhǎng)期運(yùn)行的任務(wù)。
服務(wù)的運(yùn)行不依賴于任何用戶界面,即使 APP 被切換到后臺(tái),或者打開了另外一個(gè) APP,服務(wù)仍然能夠保持正常運(yùn)行。
但是當(dāng)某個(gè) APP 進(jìn)程被殺掉時(shí),那么這個(gè) APP 所創(chuàng)建的所有服務(wù)也就停止咯。
另外,服務(wù)本身并不會(huì)自動(dòng)開啟線程,服務(wù)代碼默認(rèn)是運(yùn)行在主線程中的。所以如果需要執(zhí)行的業(yè)務(wù)邏輯耗時(shí)長(zhǎng),那么為了防止主線程被阻塞,我們必須在服務(wù)內(nèi)部創(chuàng)建子線程來執(zhí)行這些業(yè)務(wù)邏輯。
1 定義服務(wù)
在 Android Studio 中可以通過 File→New→Service→Service 來創(chuàng)建服務(wù):
在彈出的對(duì)話框中配置服務(wù):
在此配置服務(wù)名。下面兩個(gè)配置項(xiàng)說明如下:
* Exported:是否允許除了當(dāng)前程序之外的其他程序訪問這個(gè)服務(wù)。(默認(rèn)勾選)
* Enabled:是否啟用這個(gè)服務(wù) 。 (默認(rèn)勾選)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
public class FirstService extends Service { private static final String TAG = "FirstService" ; @Override public void onCreate() { super .onCreate(); Log.d(TAG, "onCreate" ); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(TAG, "onStartCommand" ); return super .onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super .onDestroy(); Log.d(TAG, "onDestroy" ); } public FirstService() { } @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. throw new UnsupportedOperationException( "Not yet implemented" ); } } |
創(chuàng)建好后的服務(wù)繼承自 Service,并且需要實(shí)現(xiàn) onBind() 方法。我們?cè)诖诉€重寫了以下幾個(gè)方法:
方法 | 說明 |
---|---|
void onCreate() | 服務(wù)創(chuàng)建時(shí)調(diào)用該方法。 |
onStartCommand(Intent intent, int flags, int startId) | 每次服務(wù)啟動(dòng)時(shí)調(diào)用該方法。 |
void onDestroy() | 服務(wù)銷毀時(shí)調(diào)用該方法。 |
我們還在這些方法中加入了日志,便于觀察運(yùn)行結(jié)果。
此外,在此類的任何位置調(diào)用 stopSelf() 方法,服務(wù)就會(huì)自行停止。
定義好服務(wù)后,需要在 AndroidManifest.xml 中注冊(cè)服務(wù)(如果用的是 Android Studio,那么這一步它已經(jīng)幫我們做啦):
1
2
3
4
|
< service android:name = ".FirstService" android:enabled = "true" android:exported = "true" ></ service > |
2 啟動(dòng)或停止服務(wù)
借助 Intent,我們就可以控制服務(wù)的啟動(dòng)與停止啦O(∩_∩)O哈哈~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
final Context context = this ; findViewById(R.id.start_service).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //啟動(dòng)服務(wù) startService( new Intent(context, FirstService. class )); } }); findViewById(R.id.stop_service).setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { //停止服務(wù) stopService( new Intent(context, FirstService. class )); } }); |
這里的 startService()
和 stopService()
方法都是定義在 Context 類中,所以在活動(dòng)類中可以直接調(diào)用。
執(zhí)行結(jié)果:
D/FirstService: onCreate D/FirstService: onStartCommand D/FirstService: onDestroy
**注意:**onCreate() 在第一次創(chuàng)建服務(wù)時(shí)被調(diào)用,而 onStartCommand() 會(huì)在每次啟動(dòng)服務(wù)時(shí)被調(diào)用。
服務(wù)啟動(dòng)后,可以在 Android 的 Settings → Developer opinions → Running services 中發(fā)現(xiàn)它:
點(diǎn)擊 APP 后,可以看到服務(wù)詳情:
如果找不到 Developer opinions 選項(xiàng),請(qǐng)先在 Android 的 Settings → About emulated device → 多次點(diǎn)擊 Build number 就可以開啟開發(fā)者模式啦:
3 活動(dòng)控制服務(wù)
活動(dòng)是通過實(shí)現(xiàn) ServiceConnection
接口來與服務(wù)建立連接的,它包含以下兩個(gè)方法:
方法 | 說明 |
---|---|
onServiceConnected(ComponentName name, IBinder service) | 服務(wù)綁定后調(diào)用該方法。 |
onServiceDisconnected(ComponentName name) | 服務(wù)解綁后調(diào)用該方法。 |
而服務(wù)是通過實(shí)現(xiàn) IBinder onBind(Intent intent)
方法來轉(zhuǎn)換為 onServiceConnected() 方法所需要的 IBinder 型的 service 參數(shù)的。
首先,我們修改服務(wù)類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class FirstService extends Service { class CustomBinder extends Binder { public void init(){ Log.d(TAG, "init CustomBinder" ); } } @Override public IBinder onBind(Intent intent) { return new CustomBinder(); } } |
這里,我們定義了一個(gè)內(nèi)部類 CustomBinder,里面實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的初始化方法。然后在 onBind() 方法中返回它的實(shí)例。
接著,我們修改活動(dòng)類,創(chuàng)建 ServiceConnection 實(shí)例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.d(TAG, "onServiceConnected" ); FirstService.CustomBinder customBinder = (FirstService.CustomBinder) service; customBinder.init(); } @Override public void onServiceDisconnected(ComponentName name) { Log.d(TAG, "onServiceDisconnected" ); } }; |
現(xiàn)在,就可以通過 ServiceConnection 實(shí)例來綁定或者解綁服務(wù)啦。
綁定服務(wù):
bindService 接收三個(gè)參數(shù):
參數(shù) | 類型 | 說明 |
---|---|---|
service | Intent | 這個(gè) Intent 將綁定當(dāng)前的活動(dòng)類與服務(wù)類。 |
conn | ServiceConnection | ServiceConnection 對(duì)象。 |
flags | int | 綁定服務(wù)的方式。這里的 BIND_AUTO_CREATE 表示綁定后自動(dòng)創(chuàng)建服務(wù)。 |
【綁定服務(wù)】輸出結(jié)果:
D/FirstService: onCreate D/MainActivity: onServiceConnected D/FirstService: init CustomBinder
解綁服務(wù):
1
|
unbindService(connection); |
只要傳入 ServiceConnection 對(duì)象即可解綁,是不是很簡(jiǎn)單呀 O(∩_∩)O哈哈~
【解綁服務(wù)】輸出結(jié)果:
D/FirstService: onDestroy
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/deniro_li/article/details/80791740