views:

469

answers:

6

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

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

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

public void DoSomething()
{
     //do something here
}
+1  A: 

I suggest the Timer, it allows you to schedule a method to be called on a very specific interval. This will not block your UI, and keep your app resonsive while the method is being executed.

The other option, is the wait(); method, this will block the current thread for the specified length of time. This will cause your UI to stop responding if you do this on the UI thread.

Nate Bross
Thread.sleep() is better than Object.wait(). Wait implies you expect to be notified and are synchronizing around some activity. Sleep indicates that you simply wish to do nothing for some specified time.Timer is the way to go if you want the action to happen asynchronously at some later point in time.
Tim Bender
That is true. Thats why I listed it as another option ;-)
Nate Bross
+2  A: 

http://stackoverflow.com/questions/1520887/how-to-pause-sleep-thread-or-process-in-android

Flavio
Thread.sleep ..well freezes the whole thread including potentially the UI which will become unresponsive for 5s.
OscarRyz
Updated from Thread.Sleep to this post. As said above, thread.sleep would block the UIThread - which could not be wanted.
Flavio
A: 

See this demo:

import java.util.Timer;
import java.util.TimerTask;

class Test {
     public static void main( String [] args ) {
          int delay = 5000;// in ms 

          Timer timer = new Timer();

          timer.schedule( new TimerTask(){
             public void run() { 
                 System.out.println("Wait, what..:");
              }
           }, delay);

           System.out.println("Would it run?");
     }
}
OscarRyz
+2  A: 

It looks like the Mac OS API lets the current thread continue, and schedules the task to run asynchronously. In the Java, the equivalent function is provided by the java.util.concurrent package. I'm not sure what limitations Android might impose.

private static final ScheduledExecutorServer worker = 
  Executors.newSingleThreadScheduledExecutor();

void someMethod() {
  ⋮
  Runnable task = new Runnable() {
    public void run() {
      /* Do something… */
    }
  };
  workers.schedule(task, 5, TimeUnit.SECONDS);
  ⋮
}
erickson
+4  A: 

Thanks for all the great answers, I found a solution that best suits my needs.

Handler myHandler = new DoSomething();
Message m = new Message();
m.obj = c;//passing a parameter here
myHandler.sendMessageDelayed(m, 1000);

class DoSomething extends Handler {
        @Override
        public void handleMessage(Message msg) {
          MyObject o = (MyObject) msg.obj;
          //do something here
        }
    }
aryaxt
A: 

Yeah, I would recommend using the Android-specific Handler class. I was looking into timers on the Android recently and this ended up being the best solution. I was trying to have something run at a certain interval, but it works the same way for a one-time delay. This page has all the details.

mxrider