HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaCritical

How to call a method after a delay in Android

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howandroidaftermethodcalldelay

Problem

I want to be able to call the following method after a specified delay.
In objective c there was something like:

[self performSelector:@selector(DoSomething) withObject:nil afterDelay:5];


Is there an equivalent of this method in android with java?
For example I need to be able to call a method after 5 seconds.

public void DoSomething()
{
     //do something here
}

Solution

Kotlin
Handler(Looper.getMainLooper()).postDelayed({
//Do something after 100ms
}, 100)


Java
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
@Override
public void run() {
//Do something after 100ms
}
}, 100);


The class to import is android.os.handler.

Context

Stack Overflow Q#3072173, score: 2204

Revisions (0)

No revisions yet.