- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个函数,我只是想明确地延迟返回值
private fun loadData(): DataModel? {
Handler(Looper.getMainLooper()).postDelayed(
when (fetchStyle) {
FetchStyle.FETCH_SUCCESS -> return DataModel("Data Loaded")
FetchStyle.FETCH_EMPTY -> return DataModel("")
FetchStyle.FETCH_ERROR -> throw IllegalStateException("Error Fetching")
}, 3000)
}
然而,有一个警告状态,即 postDelayed
无法访问,因此不会触发 3s 延迟。
为了使其可访问,我必须在周围添加额外的括号
private fun loadData(): DataModel? {
Handler(Looper.getMainLooper()).postDelayed({
when (fetchStyle) {
FetchStyle.FETCH_SUCCESS -> DataModel("Data Loaded")
FetchStyle.FETCH_EMPTY -> DataModel("")
FetchStyle.FETCH_ERROR -> throw IllegalStateException("Error Fetching")
}}, 3000)
}
但是,我不能再返回 DataModel 值了。我怎样才能解决这个问题以延迟 3 秒,同时我仍然可以返回相应的 DataModel 或抛出异常?
最佳答案
如果只想阻塞当前线程,可以使用Thread.sleep
:
private fun loadData(): DataModel? {
Thread.sleep(3000);
return when (fetchStyle) {
FetchStyle.FETCH_SUCCESS -> DataModel("Data Loaded")
FetchStyle.FETCH_EMPTY -> DataModel("")
FetchStyle.FETCH_ERROR -> throw IllegalStateException("Error Fetching")
}
}
使用 Handler
和 Runnable
的示例,带有单独的回调函数:
private fun loadData(): DataModel? {
Handler(Looper.getMainLooper()).postDelayed({
val result = when (fetchStyle) {
FetchStyle.FETCH_SUCCESS -> DataModel("Data Loaded")
FetchStyle.FETCH_EMPTY -> DataModel("")
FetchStyle.FETCH_ERROR -> throw IllegalStateException("Error Fetching")
}
loadDataCallback(result)
}, 3000)
}
fun useLoadData() {
loadData()
}
private fun loadDataCallback(dataModel: DataModel?) {
// use result here
}
一种更像 Kotlin 的方法,传递一个函数作为回调:
private fun loadData(callback: (DataModel?) -> Unit): DataModel? {
Handler(Looper.getMainLooper()).postDelayed({
val result = when (fetchStyle) {
FetchStyle.FETCH_SUCCESS -> DataModel("Data Loaded")
FetchStyle.FETCH_EMPTY -> DataModel("")
FetchStyle.FETCH_ERROR -> throw IllegalStateException("Error Fetching")
}
callback(result)
}, 3000)
}
fun useLoadData() {
loadData { dataModel ->
// use result here
}
}
请注意,这些示例不会阻塞任何线程,原始示例代码的 Handler(Looper.getMainLooper())
部分(我为这些示例保留的)将执行when
语句以及之后返回主线程的回调。
关于android - 此 Kotlin 代码无法访问 Handler()postDelayed,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44997227/
我正在尝试为一个应用程序循环播放一个简短的动画,我在许多网站和论坛中找到了这个解决方案或其中的几个。然而,首席开发人员告诉我这段代码最终会使应用程序崩溃,我应该改用线程。谁能详细解释一下为什么这段代码
根据Handler.postDelayed(Runnable r, long delayMillis)的文档: Causes the Runnable r to be added to the mes
我试图实现一个循环Runnable。我发现的示例似乎使用以下想法来启动可运行的。 handler = new Handler(); final Runnable r =
对于这种使用“handler.posDelayed”的类型,我没有看到真正的答案。 因此,我使用 for 循环多次执行“handler.posDelayed”。问题是,当我离开 Activity 并重
我遇到错误:无法解析符号“postDelayed”。在: handler.postDelayed(updateCurrentTime, 500); 这是我的一些代码: package com.exam
我有一个情况,我创建了一个按钮和一个像这样的函数。 ... public void BtnOnClick(View view) { displayMsg(); } ... private vo
如何将用户字符串拉入处理程序timer.postDelayed? public class Quotes extends Activity implements OnClickListener {
我需要的是当 competition.isTimerStarted 变成 false 这个 runnable 应该启动 buttonsProcedure() 一次然后自己停止但这并没有发生 - but
谁能看出这不起作用的任何原因? override fun resetAnimations() { Log.d("MainActivity", "start") Handler().p
我正在查看 postDelayed 的 Android 文档 post delayed documentation 这类似于另一个问题 - https://stackoverflow.com/ques
我正在尝试在一段时间后自行重启服务。我的代码看起来像这样(在 onStartCommand(...) 内) Looper.prepare(); Handler handler = new Handle
我正在尝试为我的应用制作启动画面。我使用了 postDelayed 方法。这是我的代码: public class SplashScreenActivity extends Activity {
我正在使用 handler.postDelayed 方法为某些动画内容创建一些延迟。 我正在使用 Mediaplayer 播放一些歌曲。用户可以通过单击下一步退出此操作类。但是在下一个屏幕上,即使我在
我正在使用此代码播放声音 final MediaPlayer mp = MediaPlayer.create(this, R.raw.sound); mp.setOnComp
我正在尝试更改 handler.postDelay() 中处理程序的延迟,但在这里它只工作一次然后停止。关键的方法在最后。我在想如何使可运行程序在 postDelay() 循环中工作。 class M
我有一个简单的秒表代码 fragment 。线程在自定义类中运行,它通过接口(interface)连接到主 Activity public class MainActivity extends App
在我的应用程序中,我使用具有多个可运行对象的处理程序。为了调用这些可运行对象,我循环 postDelayed,以便它们充当计时器。我意识到随着时间的推移,这会生成一个巨大的堆(10mb,并且还在不断增
我有一个监听蓝牙串行数据的线程。 我有一个将数据传递到 ui 线程的方法,名为:sendData(byte[] buffer),它将数据完美地发送到 UI 线程,这可能会导致它在以下情况下变得非常慢:
这是我的代码,但理论上它应该每秒发布此日志消息直到永远,但事实并非如此。有任何想法吗?我的目标是让它永远运行,但 while 循环也不起作用。 import android.app.Activity;
我想为 WebView 动态设置自动滚动速度。在 onCreate 中调用 autoScroll(25) 并将其删除,接下来调用 autoScroll(300) 但是当 apk 运行时自动滚动速度为
我是一名优秀的程序员,十分优秀!