Android/Java 中 JavaScript setInterval/setTimeout 的等价物是什么?
谁能告诉我 Android 上是否存在 setInterval/setTimeout 的等效项?有人有关于如何做到这一点的例子吗?
Can anyone tell me if an equivalent for setInterval/setTimeout exists for Android? Does anybody have any example about how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
与往常一样,Android 有很多方法可以做到这一点,但假设您只是想稍后在同一个线程上运行一段代码,我使用这个:
.. 这几乎相当于
As always with Android there's lots of ways to do this, but assuming you simply want to run a piece of code a little bit later on the same thread, I use this:
.. this is pretty much equivalent to
setInterval()
函数每 n 毫秒重复一次
Javascript
近似 java 等效
setTimeout()
函数,仅有效n 毫秒后
Javascript
近似 java 等价
setInterval()
function that repeats itself in every n milliseconds
Javascript
Approximate java Equivalent
setTimeout()
function that works only after n milliseconds
Javascript
Approximate java Equivalent
如果您不担心唤醒手机或让应用程序起死回生,请尝试:
If you're not worried about waking your phone up or bringing your app back from the dead, try:
根据您实际想要实现的目标,您应该查看 Android 处理程序:
http://developer。 android.com/reference/android/os/Handler.html
如果您之前使用 javascript setTimeout() 等来安排任务在将来运行,这就是 Android 的执行方式(postDelayed / sendMessageDelayed)。
请注意,处理程序或计时器都不会使 Android 手机从睡眠模式中唤醒。换句话说,如果你想安排一些实际发生的事情,即使屏幕关闭/CPU 正在睡眠,你也需要检查 AlarmManager。
Depending on what you actually want to achieve, you should take a look at Android Handlers:
http://developer.android.com/reference/android/os/Handler.html
If you previously used javascript setTimeout() etc to schedule a task to run in the future, this is the Android way of doing it (postDelayed / sendMessageDelayed).
Note that neither Handlers or Timers makes an Android phone wake up from sleep mode. In other words, if you want to schedule something to actually happen even though the screen is off / cpu is sleeping, you need to check out the AlarmManager too.
第一个答案绝对是正确的答案,也是我基于此 lambda 版本的基础,它的语法要短得多。由于 Runnable 只有 1 个重写方法“run()”,我们可以使用 lambda:
The first answer is definitely the correct answer and is what I based this lambda version off of, which is much shorter in syntax. Since Runnable has only 1 override method "run()", we can use a lambda:
我对 JavaScript 不太了解,但我认为计时器可能就是您正在寻找的。
http://developer.android.com/reference/java/util/Timer.html
来自链接:
一次性计划在绝对时间或相对延迟后运行。重复任务按固定周期或固定速率进行安排。
I do not know much about JavaScript, but I think Timers may be what you are looking for.
http://developer.android.com/reference/java/util/Timer.html
From the link:
One-shot are scheduled to run at an absolute time or after a relative delay. Recurring tasks are scheduled with either a fixed period or a fixed rate.
我正在为 android 创建一个 mp3 播放器,我想每 500ms 更新一次当前时间,所以我像
setInterval
那样递归地调用相同的方法
I was creating a mp3 player for android, I wanted to update the current time every 500ms so I did it like this
setInterval
which calls the same method recursively
这是一个 setTimeout 等效项,在尝试更新用户界面时最有用
延迟之后。
如您所知,更新用户界面只能通过 UI 线程完成。
AsyncTask 通过从该线程调用其 onPostExecute 方法来为您完成此操作。
Here's a setTimeout equivalent, mostly useful when trying to update the User Interface
after a delay.
As you may know, updating the user interface can only by done from the UI thread.
AsyncTask does that for you by calling its onPostExecute method from that thread.
与 Android 一样,有很多方法可以做到这一点,但假设您只是想在同一个线程上运行一段代码,我使用这个:
此代码将每秒记录 Hai Codemaker 文本。
As always with Android there's lots of ways to do this, but assuming you simply want to run a piece of code on the same thread, I use this:
This code will log Hai Codemaker text every one second.
如果有人想要 -
Kotlin 相当于 JavaScript setInterval/setTimeout
重要:请记住导入
android.os.Handler
。不要被java.util.logging.Handler
超时等效
Javascript: setTimeout()
Kotlin: runOnTimeout()
Kotlin 搞错了: Calling
Timeinterval 等价
Javascript: setInterval()
Kotlin: runOnInterval()
Kotlin: Calling
可取消的超时和间隔
如果你想使用自定义处理程序,以便您可以取消可运行,然后您可以使用以下代码。
超时
超时:呼叫
间隔
间隔:呼叫
In case someone wants -
Kotlin equivalent to JavaScript setInterval/setTimeout
IMPORTANT: Remember to import
android.os.Handler
. Don't get mistaken byjava.util.logging.Handler
Timeout equivalent
Javascript: setTimeout()
Kotlin: runOnTimeout()
Kotlin: Calling
Timeinterval equivalent
Javascript: setInterval()
Kotlin: runOnInterval()
Kotlin: Calling
Cancellable timeout and interval
If you want to use custom handler so that you can cancel the runnable, then you can use following codes.
Timeout
Timeout: Calling
Interval
Interval: Calling
Kotlin:
您还可以使用 CountDownTimer:
并在您的代码中:
Kotlin:
You can also use CountDownTimer:
And in your code: