日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android必须服务,说说在Android如何使用服务(Service)的方法

發(fā)布時(shí)間:2025/3/15 Android 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android必须服务,说说在Android如何使用服务(Service)的方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 服務(wù)(Service)適合執(zhí)行那些不需要和用戶交互而且還要求長期運(yùn)行的任務(wù)。

服務(wù)的運(yùn)行不依賴于任何用戶界面,即使 APP 被切換到后臺,或者打開了另外一個(gè) APP,服務(wù)仍然能夠保持正常運(yùn)行。

但是當(dāng)某個(gè) APP 進(jìn)程被殺掉時(shí),那么這個(gè) APP 所創(chuàng)建的所有服務(wù)也就停止咯。

另外,服務(wù)本身并不會自動(dòng)開啟線程,服務(wù)代碼默認(rèn)是運(yùn)行在主線程中的。所以如果需要執(zhí)行的業(yè)務(wù)邏輯耗時(shí)長,那么為了防止主線程被阻塞,我們必須在服務(wù)內(nèi)部創(chuàng)建子線程來執(zhí)行這些業(yè)務(wù)邏輯。

1 定義服務(wù)

在 Android Studio 中可以通過 File→New→Service→Service 來創(chuàng)建服務(wù):

在彈出的對話框中配置服務(wù):

在此配置服務(wù)名。下面兩個(gè)配置項(xiàng)說明如下:

* Exported:是否允許除了當(dāng)前程序之外的其他程序訪問這個(gè)服務(wù)。(默認(rèn)勾選)

* Enabled:是否啟用這個(gè)服務(wù) 。 (默認(rèn)勾選)

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() 方法。我們在此還重寫了以下幾個(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ù)就會自行停止。

定義好服務(wù)后,需要在 AndroidManifest.xml 中注冊服務(wù)(如果用的是 Android Studio,那么這一步它已經(jīng)幫我們做啦):

android:name=".FirstService"

android:enabled="true"

android:exported="true">

2 啟動(dòng)或停止服務(wù)

借助 Intent,我們就可以控制服務(wù)的啟動(dòng)與停止啦O(∩_∩)O哈哈~

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() 會在每次啟動(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),請先在 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ù)類:

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è)簡單的初始化方法。然后在 onBind() 方法中返回它的實(shí)例。

接著,我們修改活動(dòng)類,創(chuàng)建 ServiceConnection 實(shí)例:

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(new Intent(context, FirstService.class), connection, BIND_AUTO_CREATE);

bindService 接收三個(gè)參數(shù):

參數(shù)

類型

說明

service

Intent

這個(gè) Intent 將綁定當(dāng)前的活動(dòng)類與服務(wù)類。

conn

ServiceConnection

ServiceConnection 對象。

flags

int

綁定服務(wù)的方式。這里的 BIND_AUTO_CREATE 表示綁定后自動(dòng)創(chuàng)建服務(wù)。

【綁定服務(wù)】輸出結(jié)果:

D/FirstService: onCreate D/MainActivity: onServiceConnected D/FirstService: init CustomBinder

解綁服務(wù):

unbindService(connection);

只要傳入 ServiceConnection 對象即可解綁,是不是很簡單呀 O(∩_∩)O哈哈~

【解綁服務(wù)】輸出結(jié)果:

D/FirstService: onDestroy

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的android必须服务,说说在Android如何使用服务(Service)的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。