Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 272 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handler to run task every 5 seconds Kotlin

#1
I would like to run a certain code every 5 seconds. I am having trouble achieving this with a handler. How can this be done in Kotlin? Here is what I have so far. Also to note, the variable Timer_Preview is a Handler.

[![My Code][1]][1]


[1]:
Reply

#2
Since you can't reference a lambda you're currently in, and you can't reference the property you're defining while you're defining the lambda you're assigning to it, the best solution here is an [`object` expression](

[To see links please register here]

):

val runnableCode = object: Runnable {
override fun run() {
handler.postDelayed(this, 5000)
}
}

Assuming that this property is not a `var` because you actually want to change it while this self-calling is happening.
Reply

#3
As Kotlin does not yet allow recursive lambdas (see [KT-10350](

[To see links please register here]

)), you must use other constructs, such as object expressions as in @zsmb13's answer, or ordinary functions as below

fun StartTimer() {
Timer_Preview.postDelayed(Runnable { runnable() }, 5000)
}

fun runnable() {
//Code here

// Run code again after 5 seconds
Timer_Preview.postDelayed(Runnable { runnable() }, 5000)
}

However, in your particular case, it looks like you could just call `StartTimer()` again to re-arm the timer, assuming it doesn't do anything else:

private val RunnableCode = Runnable {
//Code here

//Run code again after 5 seconds
StartTimer()
}
Reply

#4
You can do this with simple functions:

private fun waitToDoSomethingRecursively() {
handler.postDelayed(::doSomethingRecursively, 5000)
}

private fun doSomethingRecursively () {
...
waitToDoSomethingRecursively()
}
Reply

#5
Simply Use `fixedRateTimer`

fixedRateTimer("timer",false,0,5000){
[email protected] {
Toast.makeText(this@MainActivity, "text", Toast.LENGTH_SHORT).show()
}
}

Change initial delay by setting another value for the third parameter.
Reply

#6
I recommended **SingleThread** because it is very useful. If you would like to do job for each second, you can set because parameters of it:

> Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

TimeUnit values are: NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS.

**Example:**

private fun mDoThisJob(){

Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate({
//TODO: You can write your periodical job here..!

}, 1, 1, TimeUnit.SECONDS)
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through