Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...