3

My app contains a ViewFlipper with some images. when app starts, the ViewFlipper startflipping(). When user touch the screen ViewFlipper stopflipping(). I must do that after 60 seconds from last touch, ViewFlipper to start again flipping. My class implements onTouchListener and I have this method onTouch:

public boolean onTouch(View arg0, MotionEvent arg1) {


        switch (arg1.getAction()) {
        case MotionEvent.ACTION_DOWN: {

            downXValue = arg1.getX();
            break;
        }

        case MotionEvent.ACTION_UP: {

            currentX = arg1.getX();


            if (downXValue < currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_right_in));
                // Flip!
                vf.showPrevious();
            }


            if (downXValue > currentX) {
                // Set the animation
                vf.stopFlipping();
                vf.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_out));
                vf.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.push_left_in));
                // Flip!
                vf.showNext();
            }

            if (downXValue == currentX) {
                final int idImage = arg0.getId();

                vf.stopFlipping();
                System.out.println("id" + idImage);
                System.out.println("last touch "+getTimeOfLastEvent());

            }
            break;
        }
        }

        // if you return false, these actions will not be recorded
        return true;
    }

and I found this method, for finding time of the last touch :

static long timeLastEvent=0;
public long getTimeOfLastEvent() {

        long duration = System.currentTimeMillis() - timeLastEvent;
        timeLastEvent = System.currentTimeMillis();
        return duration;
    }

My question is : where should I call the getTimeOfLastEvent() ? If I put it on onTouch() I will never catch the moment when getTimeOfLastEvent==60000, right?

Gabrielle
  • 4,933
  • 13
  • 62
  • 122

1 Answers1

5

What you should do is create a Handler (should be an instance variable of your Activity and should be initialized during onCreate):

Handler myHandler = new Handler();

Also you will need a Runnable that can start the flipping again (also needs to be declared within your Activity):

private Runnable flipController = new Runnable() {
  @Override
  public void run() {
    vf.startFlipping();
  }
};

Then in your onClick you just post the Runnable on the Handler but delayed by 60 seconds:

myHandler.postDelayed( flipController, 60000 );

Posting it delayed means: "Run this code in 60 seconds".

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67