How to make a action happen in android if nothing happen with in 5 second?

269 Views Asked by At
  final Handler handler = new Handler();
    final Runnable timer = new Runnable() {
        @Override
        public void run() {
            // user too late: increment miss counter
            if (++secondmisses >= MISS_LIMIT) {
                //TODO miss limit reached
                finish(); // close this activity
            }
        }
    };

This is my runnable timer, not sure if I can find the solution here

I want to let my sphero detect collision with 5 second

if (got collision in 5 seconds) move up, if they say is no collision, move down

This is my code below

I not sure what else I can make it happen

       mRobot.drive(0.0f, ROBOT_VELOCITY);
            handler.removeCallbacks(timer);
            handler.postDelayed(timer, ONE_SECOND);

        this.checkcollision(v, 1); // if there is no collision
        this.checkcollisoon(v,2); //if there is a collision

Please help :)

1

There are 1 best solutions below

0
Sunshinator On BEST ANSWER

Try with CountDownTimer. Something like this:

long duration = 12345;
long ticksInterval = 1000; // 1 second in millis

new CountDownTimer(duration, ticksInterval){
    public void onTick(long remaining) {
        // Do something each ticksInterval millis
    }

    public void onFinish() {
        // Do something after duration millis
    }
}.start();

When something happens in your app, you can set a non static class boolean and test it in onFinish() to see if something happened during the timer.