What is the equivalent to a JavaScript setInterval/setTimeout in Android/Java?

前端 未结 10 1178
遥遥无期
遥遥无期 2020-11-29 16:54

Can anyone tell me if an equivalent for setInterval/setTimeout exists for Android? Does anybody have any example about how to do it?

10条回答
  •  没有蜡笔的小新
    2020-11-29 17:48

    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.

提交回复
热议问题