- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个 Kotlin 多平台项目。我正在尝试使用计时器和倒数计时器,但我无法访问 kotlin.concurrent.fixedRateTimer
或 import kotlin.concurrent.timer
在 commonMain
模块。
然而 kotlin.concurrent
可用:
这是根build.gradle
:
plugins {
kotlin("multiplatform")
id("com.android.library")
id("kotlin-android-extensions")
}
// ...
kotlin {
//...
sourceSets {
val commonMain by getting {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.10")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.10")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9")
//...
}
}
//...
}
}
我想知道是否甚至可以在那里使用这些方法。如果没有,如何在
commonMain
中编写计时器和倒数计时器?模块?
Coroutines
实现相同的功能但由于它们不精确而失败:
fun doAfter(delay: Long, action: () -> (Unit)) = launch {
delay(delay)
action.invoke()
}
fun countdown(time: Long, tick: Long, onTick: () -> (Unit), onFinish: () -> (Unit)) = launch {
val ticks = (time / tick).toInt()
repeat(ticks) {
onTick()
delay(tick)
}
onFinish()
}
最佳答案
正如 Qaz 所说,您尝试在公共(public)代码中使用的函数是 JVM only .
通常在 KMP 中,当您仍然没有框架内置的通用功能时,您可以采用不同的方法:
expect
/ actual
使用 native 框架类机制commonMain:Timer.kt
expect class KMMTimer(
name: String? = null,
interval: Long,
delay: Long,
action: () -> Unit
) {
val name: String?
val interval: Long
val delay: Long
fun start()
fun cancel()
fun isRunning(): Boolean
}
androidMain:Timer.kt
import java.util.*
import kotlin.concurrent.fixedRateTimer
actual class KMMTimer actual constructor(
actual val name: String?,
actual val interval: Long,
actual val delay: Long,
action: () -> Unit
) {
private var timer: Timer? = null
private val action = action
actual fun start() {
if (!isRunning()) {
timer = fixedRateTimer(
name = name,
initialDelay = delay,
period = interval
) {
action()
}
}
}
actual fun cancel() {
timer?.cancel()
timer = null
}
actual fun isRunning(): Boolean {
return timer != null
}
}
iosMain:Timer.kt
import platform.Foundation.NSDate
import platform.Foundation.NSRunLoop
import platform.Foundation.NSRunLoopCommonModes
import platform.Foundation.NSTimer
actual class KMMTimer actual constructor(
actual val name: String?,
actual val interval: Long,
actual val delay: Long,
action: () -> Unit
) {
private var timer: NSTimer? = null
private var action = action
actual fun start() {
if (!isRunning()) {
timer = NSTimer(
fireDate = NSDate(
NSDate().timeIntervalSinceReferenceDate + (delay.toDouble() / 1000)
),
interval = (interval.toDouble() / 1000),
repeats = true,
block = {
action()
}
)
timer?.let {
NSRunLoop.currentRunLoop().addTimer(it, NSRunLoopCommonModes)
}
}
}
actual fun cancel() {
timer?.invalidate()
timer = null
}
actual fun isRunning(): Boolean {
return timer != null
}
}
关于kotlin - 无法在 Kotlin Multiplatform 中访问 fixedRateTimer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64458126/
我正在第一次使用 Kotlin Coroutines 从事一个爱好项目。我已经阅读并观看了有关它的视频,我有点了解这个概念。但我遇到了一个问题。让我告诉你我的代码。 package com.dev.t
我正在开发一个 Kotlin 多平台项目。我正在尝试使用计时器和倒数计时器,但我无法访问 kotlin.concurrent.fixedRateTimer或 import kotlin.concurr
我在 Kotlin 中有一个简单的应用程序,它绘制一个矩形,然后使用 fixedRateTimer 每秒更新矩形的位置 30 次。我遇到的问题是,当我关闭显示矩形的窗口时,应用程序继续运行,我必须按下
我有一个计时器设置为在 Activity 处于 Activity 状态时每 20 秒记录一次时间戳。它正确地注册了时间,但是当我的 Activity 的 onStop 方法被触发时,它会继续运行。我已
我是一名优秀的程序员,十分优秀!