網(wǎng)站首頁 編程語言 正文
service 是什么
Service是實(shí)現(xiàn)程序后臺運(yùn)行的解決方案,適合執(zhí)行非交互,后臺預(yù)先的任務(wù),即使用戶打開其他應(yīng)用,Service也能夠正常運(yùn)行
Service需要內(nèi)部手動創(chuàng)建子線程
多線程編程
用法:
(1) 繼承的方式(耦合較高,不推薦)
class MyThread : Thread() { override fun run () { // 編寫具體邏輯 } } // 啟動 MyThread().start()
(2) Runnable接口定義一個線程
class MyThread : Runnable { override fun run () { // 子線程具體邏輯 } } // 啟動 val myThread = MyThread() Thread(myThread).start()
簡化寫法:如果你不想專門定義一個類去實(shí)現(xiàn)Runnable接口, 可以使用Lambda方式
Thread { // 編寫具體邏輯 }.start()
更加簡化的寫法:
thread { // 編寫具體的邏輯 }
在子線程中更新UI
Android 的UI 也是線程不安全的, 更新應(yīng)用程序的UI元素, 必須在主線程中進(jìn)行, 否則會出現(xiàn)異常
如果在子線程中直接更新UI,會出現(xiàn)崩潰,提示如下錯誤
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
那子線程如何更新UI呢?
通過異步消息傳遞給主線程, 在主線程更新UI 修改MainActivity.kt
class MainActivity : AppCompatActivity() { val sign = 1 val handler = object: Handler(Looper.getMainLooper()) { override fun handleMessage(msg: Message) { when (msg.what) { sign -> textView.text = "Nice to meet you 2" } } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.setOnClickListener{ thread { val msg = Message() msg.what = sign handler.sendMessage(msg) } } } }
定義一個Handler對象,重寫handleMessage方法
如果Message(android.os.Message)的what字段等于sign,就將UI更新
異步消息處理機(jī)制
(1) Message 線程中傳遞少量消息,使用arg1和arg2攜帶整型數(shù)據(jù), obj字段攜帶Obejct對象
(2) Handler,用于發(fā)送和接收消息
發(fā)送: 使用sendMessage() post() 方法
接受: 最終會傳遞到handleMessage方法中
(3) MessageQueue, 消息隊(duì)列,存放Handler發(fā)送的消息,等待被處理,每個線程只會有一個MessageQueue
(4) Looper, 是每個線程中MessageQueue的管家, 調(diào)用Looper的 loop()
方法后,會進(jìn)入無限循環(huán)中,每當(dāng)發(fā)現(xiàn)MessageQueue中存在一條消息,就取出,并傳遞到Handler的handleMessage方法中,每個線程用一個Looper對象
異步消息處理流程:
1 主線程創(chuàng)建handler對象, 重寫handleMessage方法
2 當(dāng)子線程中需要進(jìn)行UI操作,就創(chuàng)建一個Message對象,通過Handler 的sandMessage方法將消息發(fā)送出去,消息被添加到MessageQueue中等待
3 Looper一直嘗試從MessageQueue中取消息,最后分發(fā)給Handler的handlerMessage方法中,由于Handler函數(shù)中傳入了Looper.getMainLooper(), 此時handleMessage() 方法中的代碼會在主線程中運(yùn)行
4 使用AsyncTask
為了方便子線程對UI操作, Android提供了一些好用的工具如AsyncTask,原來也是基于異步消息處理
(1)基本用法:
AsyncTask是一個抽象類,如果想使用它,需要一個子類繼承,可以在繼承時指定3個泛型參數(shù): params: 可在后臺任務(wù)中使用 progress :在后臺任務(wù)執(zhí)行時, 如果需要在界面上顯示的進(jìn)度,使用泛型作為進(jìn)度單位 Result 任務(wù)執(zhí)行完后, 對結(jié)果進(jìn)行返回, 返回泛型類型
最簡單的形式:
class DownloadTask :AsyncTask<Unit, Int, Boolean> () { }
當(dāng)前是一個空任務(wù),無任何實(shí)際操作,需要重寫4個方法:
1 onPreExecute() 在任務(wù)執(zhí)行前調(diào)用,用于初始化操作
2 doInBackground(Params…) 在子線程中執(zhí)行, 執(zhí)行具體耗時任務(wù)
3 onProgressUpdate(Progress…) 后臺任務(wù)調(diào)用,進(jìn)行UI操作
4 onPostExecute(Result) 后臺任務(wù)執(zhí)行完畢并通過return返回時, 收尾工作
Service 基本用法
1.定義一個Service
新建一個ServiceTest項(xiàng)目
右擊 com.example.servicetest -> New -> Service -> Service
類名改成MyService, Exported表示將Service暴露給外部訪問
Enable表示啟用這個Service
生成如下代碼:
class MyService : Service() { override fun onBind(intent: Intent): IBinder { TODO("Return the communication channel to the service.") } }
MyService 繼承自Service類, 有一個onBind方法,是Service唯一抽象方法,需要在子類實(shí)現(xiàn)
重寫一些方法:
- onCreate() service創(chuàng)建調(diào)用
- onStartCommand() 每次service啟動調(diào)用
- onDestory() 銷毀調(diào)用
ps: Service需要在AndroidManifest.xml文件中注冊(在創(chuàng)建service中會自動注冊)
啟動和停止Service
要借助Intent實(shí)現(xiàn),在ServiceTest中啟動停止MyService
添加兩個按鈕:
startServiceBtn.setOnClickListener { val intent = Intent(this, MyService::class.java) startService(intent) // 啟動Service } stopServiceBtn.setOnClickListener { val intent = Intent(this, MyService::class.java) stopService(intent) // 停止Service }
startService
和stopService
都定義在Context類中,可以直接調(diào)用
另外可以自我停止:
在service內(nèi)部調(diào)用stopSelf()
方法
啟動后可以在: 應(yīng)用 -》顯示系統(tǒng)應(yīng)用 中找到
Android8.0后,應(yīng)用在前臺,service才能穩(wěn)定運(yùn)行,否則隨時可能被系統(tǒng)回收
Activity 與 Service通信: onBind 方法
查看下載進(jìn)度:,創(chuàng)建一個專門的Binder對象管理下載功能:
private val mBinder = DownloadBinder() class DownloadBinder : Binder() { fun startDownload() { Log.d("MyService", "startDownload executed") } fun getProgress(): Int{ Log.d("MyService", "getProgress executed") return 0 } } override fun onBind(intent: Intent): IBinder { return mBinder }
當(dāng)一個Activity 和Service 綁定了之后,就可以調(diào)用該Service 里的Binder提供的方法了。
在activity中,修改:
lateinit var downloadBinder: MyService.DownloadBinder private val connection = object : ServiceConnection { override fun onServiceConnected(name: ComponentName, service: IBinder) { downloadBinder = service as MyService.DownloadBinder downloadBinder.startDownload() downloadBinder.getProgress() } override fun onServiceDisconnected(name: ComponentName) { } } ... // 綁定service bindServiceBtn.setOnClickListener { val intent = Intent(this, MyService::class.java) bindService(intent, connection, Context.BIND_AUTO_CREATE) // 綁定Service } // 解綁service unbindServiceBtn.setOnClickListener { unbindService(connection) // 解綁Service }
bindService()方法接收3個參數(shù)
第一個是Intent對象
第二個是ServiceConnection的實(shí)例
第三個是一個標(biāo)志位 BIND_AUTO_CREATE 表示在Activity 和Service 進(jìn)行綁定后
自動創(chuàng)建Service
這會使得MyService 中的onCreate()方法得到執(zhí)行
原文鏈接:https://blog.csdn.net/change_fate/article/details/128784958
相關(guān)推薦
- 2022-08-16 React?中的?setState?是同步還是異步_React
- 2022-08-04 GoFrame框架gcache的緩存控制淘汰策略實(shí)踐示例_Golang
- 2022-02-23 在項(xiàng)目中全局自動加載默認(rèn)圖的技巧
- 2023-06-03 C#利用后綴表達(dá)式解析計(jì)算字符串公式_C#教程
- 2022-08-17 python數(shù)據(jù)可視化matplotlib繪制折線圖示例_python
- 2022-12-07 C++成員函數(shù)后面加override問題_C 語言
- 2022-07-03 python編碼格式導(dǎo)致csv讀取錯誤問題(csv.reader,?pandas.csv_read)
- 2022-09-05 Golang中的Interface詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支