Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Java by (4k points)

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 }

1 Answer

0 votes
by (46k points)

In Kotlin try:

Handler().postDelayed({

  //Do something after 5000ms

}, 5000)

 

And, In Java try:

final Handler handler = new Handler();

handler.postDelayed(new Runnable() {

  @Override

  public void run() {

    //Do something after 5000ms

  }

}, 5000);

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 15, 2019 in Java by Suresh (3.4k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...