Bot*_*yok 9 android android-lifecycle kotlin kotlin-coroutines
这会阻塞 UI 线程,但如果我使用 GlobalScope,则 UI 不会被阻塞。
lifecycleScope.launch {
activity?.runOnUiThread {
Toast.makeText(activity, getString(R.string.txt_savinginprogress), Toast.LENGTH_SHORT).show()
}
val fileName = "Picture" + System.currentTimeMillis().toString()
val folderName = "BucketList"
val bitmap: Bitmap? = photoURI?.let { it1 -> getBitmapFromUri(it1) }
activity?.let {
bitmap?.let { it1 ->
PhotoSaveHelper(it).savePhoto(fileName, folderName, it1)
}
}
activity?.runOnUiThread {
Toast.makeText(activity, getString(R.string.txt_saved), Toast.LENGTH_SHORT).show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*lix 10
lifecycleScope.launch{}main默认在线程上执行里面的代码。尝试使用以下代码在 IO 调度程序中启动协程(适用于长时间运行和密集型任务)。
lifecycleScope.launch(Dispatchers.IO) {
withContext(Dispatchers.Main) {
Toast.makeText(activity, getString(R.string.txt_savinginprogress), Toast.LENGTH_SHORT).show()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5201 次 |
| 最近记录: |