Kotlin Service服務(wù)組件開(kāi)發(fā)詳解
服務(wù)簡(jiǎn)介
服務(wù)是Android中的四大組件之一,它能夠長(zhǎng)期在后臺(tái)運(yùn)行且不提供用戶(hù)界面。即使用戶(hù)切到另一應(yīng)用程序,服務(wù)仍可以在后臺(tái)運(yùn)行。
服務(wù)的創(chuàng)建
(1)創(chuàng)建Service子類(lèi)
class MyService : Service() {
override fun onBind(intent: Intent): IBinder {
TODO("Return the communication channel to the service.")
}
}(2)在清單文件中配置
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
</service>服務(wù)的啟動(dòng)方式
(1)通過(guò)startService()方法啟動(dòng)
當(dāng)通過(guò)startService()方法啟動(dòng)服務(wù)時(shí),需要自身調(diào)用stopSelf()方法或者其他組件調(diào)用stopService()方法時(shí)服務(wù)才能停止。
(2)通過(guò)bindService()方法啟動(dòng)
當(dāng)使用bingService()方法啟動(dòng)服務(wù)時(shí),需要調(diào)用unbindService()方法解除綁定之后就會(huì)被銷(xiāo)毀。
(3)即調(diào)用startService()方法,又調(diào)用了bingService()方法
這種情況下,要同時(shí)調(diào)用stopService()和unbindService()方法。
Service的生命周期
- onCreate():第一次創(chuàng)建服務(wù)時(shí)執(zhí)行的方法。
- onDestory():服務(wù)被銷(xiāo)毀時(shí)執(zhí)行的方法。
- onStartCommand():訪(fǎng)問(wèn)者通過(guò)startService(intent)啟動(dòng),服務(wù)時(shí)執(zhí)行的方法。
- onBind():使用bindService()方式啟動(dòng)服務(wù)調(diào)用的方法。
- onUnbind():解除綁定時(shí)調(diào)用的方法。

Activity和Service進(jìn)行通信
Activity和Service之間的通信由IBinder負(fù)責(zé),在A(yíng)ctivity中,創(chuàng)建一個(gè)類(lèi)實(shí)現(xiàn)ServiceConnection接口,并且在這個(gè)類(lèi)中重寫(xiě)onServiceConnected方法(當(dāng)Service被綁定時(shí)會(huì)回調(diào)這個(gè)方法)和onServiceDisconnected方法(Service的創(chuàng)建進(jìn)程崩潰或者被殺掉才會(huì)調(diào)用),然后再綁定Service。
class MainActivity : AppCompatActivity() {
lateinit var myBinder:MyService.mBinder
private val connection=object :ServiceConnection{
override fun onServiceConnected(p0: ComponentName?, p1: IBinder?) {
myBinder=p1 as MyService.mBinder
myBinder.a()
}
override fun onServiceDisconnected(p0: ComponentName?) {
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val bindbutton:Button=findViewById(R.id.bindbutton)
val unbindbutton:Button=findViewById(R.id.unbindbutton)
bindbutton.setOnClickListener {
val intent=Intent(this,MyService::class.java)
bindService(intent,connection,Context.BIND_AUTO_CREATE)//綁定Service
}
unbindbutton.setOnClickListener {
unbindService(connection)//解綁Service
}
}
}在Service中,需要?jiǎng)?chuàng)建一個(gè)類(lèi)繼承Binder,在onBind()方法中返回這個(gè)類(lèi)的實(shí)例。
class MyService : Service() {
private val myBinder=mBinder()
class mBinder:Binder(){
fun a(){
Log.d("data","service")
}
}
override fun onBind(intent: Intent): IBinder {
return myBinder
}
override fun onCreate() {
super.onCreate()
Log.d("data","onCreate")
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("data","onStartCommand")
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
Log.d("data","onDestroy")
}
}實(shí)現(xiàn)前臺(tái)Service
前臺(tái)服務(wù)執(zhí)行一些用戶(hù)能注意到的操作。

代碼如下:
需要先進(jìn)行權(quán)限聲明
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
class MyService : Service() {
private val myBinder=mBinder()
class mBinder:Binder(){
fun a(){
Log.d("data","service")
}
}
override fun onBind(intent: Intent): IBinder {
return myBinder
}
override fun onCreate() {
super.onCreate()
Log.d("data","onCreate")
val manager=getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
val channel=NotificationChannel("my_service","前臺(tái)Service通知",NotificationManager.IMPORTANCE_DEFAULT)
manager.createNotificationChannel(channel)
}
val intent=Intent(this,MainActivity::class.java)
val pi=PendingIntent.getActivity(this,0,intent,0)
val notification=NotificationCompat.Builder(this,"my_service")
.setContentTitle("這是主題")
.setContentText("這是內(nèi)容")
.setSmallIcon(R.drawable.ic_baseline_favorite_border_24)
.build()
startForeground(1,notification)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("data","onStartCommand")
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
Log.d("data","onDestroy")
}
}到此這篇關(guān)于Kotlin Service服務(wù)組件開(kāi)發(fā)詳解的文章就介紹到這了,更多相關(guān)Kotlin Service內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要介紹了react native中的聊天氣泡及timer封裝成的發(fā)送驗(yàn)證碼倒計(jì)時(shí)的相關(guān)資料,需要的朋友可以參考下2017-08-08
Android控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果
這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時(shí)間軸的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11
Android下使用TCPDUMP實(shí)現(xiàn)數(shù)據(jù)抓包教程
這篇文章主要介紹了Android下使用TCPDUMP實(shí)現(xiàn)數(shù)據(jù)抓包教程,本文講解使用抓包工具tcpdump抓取數(shù)據(jù),然后使用Wireshark來(lái)分析數(shù)據(jù),需要的朋友可以參考下2015-02-02
Android BottomSheet實(shí)現(xiàn)可拉伸控件
這篇文章主要為大家詳細(xì)介紹了Android BottomSheet實(shí)現(xiàn)可拉伸控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法分析
這篇文章主要介紹了Android Activity之間相互調(diào)用與傳遞參數(shù)的原理與用法,較為詳細(xì)的分析了Android組件的構(gòu)成以及Activity的創(chuàng)建、調(diào)用、切換等相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
android studio 一直卡在Gradle:Build Running的幾種解決辦法
這篇文章主要介紹了android studio 一直卡在Gradle:Build Running的解決辦法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10
android 手機(jī)SD卡讀寫(xiě)操作(以txt文本為例)實(shí)現(xiàn)步驟
要完成SD卡讀寫(xiě)操作首先對(duì)manifest注冊(cè)SD卡讀寫(xiě)權(quán)限其次是創(chuàng)建一個(gè)對(duì)SD卡中文件讀寫(xiě)的類(lèi)寫(xiě)一個(gè)用于檢測(cè)讀寫(xiě)功能的的布局然后就是UI的類(lèi)了,感興趣的朋友可以參考下,希望可以幫助到你2013-02-02
Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式
由于公司需要一個(gè)帶刻度的進(jìn)度條樣式,因?yàn)榭潭刃枰獎(jiǎng)討B(tài)去改變,所以換背景圖片的方案肯定是不行的,唯一的辦法就是自己繪制一個(gè)進(jìn)度條,下面小編給大家?guī)?lái)了Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式及實(shí)例代碼,需要的朋友參考下吧2019-10-10
Flutter開(kāi)發(fā)之路由與導(dǎo)航的實(shí)現(xiàn)
這篇文章主要介紹了Flutter開(kāi)發(fā)之路由與導(dǎo)航的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android利用代碼控制設(shè)備上其他音樂(lè)播放器的方法
這篇文章主要給大家介紹了關(guān)于A(yíng)ndroid利用代碼如何控制設(shè)備上其他音樂(lè)播放器的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06

