views:

83

answers:

5

Hello! I am trying to make a set of measurements of signal strength, so i want to make a delay between same method (that return needed value) execution - value1...delay....value2....delay.... Currently i am using

Thread.sleep(DELAY);

Such way of creating the delay seems to work, but as I understood it makes the whole app to stop. I have looked through Android Developers website and found some other ways using Timer and ScheduledExecutorService. But i do not fully understand how to create a delay using those 2 ways. May be someone will be some kind and give me some ideas or directions to start with?

+1  A: 
java.util.concurrent.Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new java.lang.Runnable()
{
  @Override
  public void run()
  {
    System.out.println("call the method that checks the signal strength here");
  }
  },
  1,
  1,
  java.util.concurrent.TimeUnit.SECONDS
 );

This is the snippet of code which will call some method after initial delay of 1 second every 1 second.

Boris Pavlović
+3  A: 

You could use a Runnable and a handler.

private Runnable mUpdateTimeTask = new Runnable() {
    public void run() {

        // Get the difference in ms
        long millis = SystemClock.uptimeMillis() - mStartTime;

        // Format to hours/minutes/seconds
        mTimeInSec = (int) (millis / 1000);

        // Do your thing

        // Update at the next second
        mHandler.postAtTime(this, mStartTime + ((mTimeInSec + 1) * 1000));
    }
};

And start this with a handler:

mHandler.postDelayed(mUpdateTimeTask, 100);

Ofcourse you have to have a global mHandler (private Handler mHandler = new Handler();) and a starting time (also the uptimeMillis). This updates every second, but you can change it for a longer period of time. http://developer.android.com/reference/android/os/Handler.html

Barry
I have 2 questions:1. Are you sure that i should use uptimeMillis for StartTime, not mStartTime = System.currentTimeMillis(); ?2. Why do you have 100 in mHandler.postDelayed?
StalkerRus
One more question. Should your code be inserted in OnCreate or just in main class?
StalkerRus
See http://developer.android.com/reference/android/os/SystemClock.html
Barry
And the runnable should just be in the Main Class, the mHandler.postDelayed where you want to start the timer. That starts in 0.1 sec, but i guess you can also you mHandler.post(mUpdateTimeTask), actually don't know why I didn't use that.
Barry
A: 

The documentation page for ScheduledExecutorService gives a good example of how to use it:

import static java.util.concurrent.TimeUnit.*;
class BeeperControl {
  private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

  public void beepForAnHour() {
    final Runnable beeper = new Runnable() {
      public void run() {
        System.out.println("beep");
      }
    };
    // Run the beeper Runnable every 10 seconds after a 10 second wait
    final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate( beeper, 10, 10, SECONDS ) ;

    // Schedule something to cancel the beeper after an hour
    scheduler.schedule( new Runnable() {
      public void run() {
        beeperHandle.cancel(true);
      }
    }, 60 * 60, SECONDS);
  }
}
tim_yates
+1  A: 

There is a tutorial about how to create a simple android Countdown timer. You can take a look, this may help.

Tony
+1  A: 

To use Timer you create a Timer instance

Timer mTimer = new Timer();

Now the task you wish to run can be scheduled.

mTimer.scheduleAtFixedRate(new TimerTask() {  
public void run() {  
//THE TASK  
}  
}, DELAY, PERIOD);

DELAY = amount of time in milliseconds before first execution.

LONG = amount of time in milliseconds between subsequent executions.

See here for more.

dorzey