我有一个前台服务,可以延迟更改壁纸,所以我在里面使用协同器,我用startService(意图(这个,MySerivce::class.java))从另一个类调用它,但是当我用stopService(意图(这个,MySerivce::class.java))停止它时,只在主线程停止上运行(比如showNotification()),下面是我的代码:
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val externalStorageState = Environment.getExternalStorageState()
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
val root=getExternalFilesDir(null)?.absolutePath
val myDir = File("$root/saved_images")
val list = myDir.listFiles()
if (myDir.exists()&&list.size>=2){
CoroutineScope(Dispatchers.IO).launch {
val time = intent?.getIntExtra("TIME",0)
if (time != null) {
manageWallpapers(list,time)
}
}
}}
showNotification()
return START_STICKY
}
}和这里的manageWallpapers函数
private suspend fun manageWallpapers(list: Array<File>?, time:Int){
while (true){
if (list != null) {
for (image in list){
setWallpaper(image)
delay(time.toLong())
}
}
}
}发布于 2021-07-12 20:31:27
您需要保留对launch返回的作业的引用,并在服务停止时调用cancel()。
private var job: Job? = null
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val externalStorageState = Environment.getExternalStorageState()
if (externalStorageState.equals(Environment.MEDIA_MOUNTED)){
val root=getExternalFilesDir(null)?.absolutePath
val myDir = File("$root/saved_images")
val list = myDir.listFiles()
if (myDir.exists()&&list.size>=2){
job = CoroutineScope(Dispatchers.IO).launch { // HERE
val time = intent?.getIntExtra("TIME",0)
if (time != null) {
manageWallpapers(list,time)
}
}
}}
showNotification()
return START_STICKY
}
}
override fun onDestroy() {
job?.cancel()
job = null
super.onDestroy()
}另一种选择是保留对CoroutineScope的引用并在其上调用cancel。
发布于 2021-07-12 21:16:51
如果您来自LifecycleService的子类,您可以使用lifecycleScope启动您的协同服务,因此当服务停止时,它将被自动取消。
build.gradle
implementation 'androidx.lifecycle:lifecycle-service:2.3.1'在您的服务中,LifecycleService的一个子类
lifecycleScope.launch {
val time = intent?.getIntExtra("TIME",0)
if (time != null) {
manageWallpapers(list,time)
}
}不需要指定Dispatchers.IO,因为您只是在调用挂起函数,而不是阻塞函数。
https://stackoverflow.com/questions/68353369
复制相似问题