HVNSweeting
HVNSweeting

Reputation: 2897

Android How do I write a simple timer for UI events?

My app needs to check whether user double clicks or not. But I can't use Android OnDoubleClickListener or something likes that. Just implements it :

My problem is diferent from "normal" double click.

What I want is: If user double clicks, run the Y activity. If user clicks 1 click only, wait 500 ms then run the X activity. If user click 2 click slowly, run the X activity

This is my code:

        long now = System.currentTimeMillis();
        thisTime = now;

        if (thisTime - lastTouchTime < WAIT_TIME) {
            // Double tap

            this.getContext()
                    .getApplicationContext()
                    .startActivity(
                            (new Intent(getContext(),
                                    ChangePlaceActivity.class))
                                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));

            // If is double tap, reset to start state.
            lastTouchTime = -1;
            return true;
        } else {
            // Too slow or first click
            // If not double tap, save last state for next check

            lastTouchTime = thisTime;

            Log.d("Worker thread", "Declare" + System.currentTimeMillis());
            Thread t = new Thread() {
                public void run()
                {
                    try {
                        sleep(WAIT_TIME);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            };
            t.start();
            try {
                t.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("Worker thread", "" + System.currentTimeMillis());

            // start MainAct
            Log.d("Single Click", "Yes");
            this.getContext()
                    .getApplicationContext()
                    .startActivity(
                            (new Intent(getContext(),

                            MainActivity.class))
                                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
        }

If use code like this, I can't got double click. If remove thread.run() thread.join(). It will start MainActivity before ChangePlacecActivity.

Is there any solution for this problems? Thank you in advance!

Sorry for my bad English.

Upvotes: 0

Views: 561

Answers (1)

dokkaebi
dokkaebi

Reputation: 9190

You can use a Handler for this. Something like:

(class-level declarations)

Handler handler = new Handler();
Runnable singleClickTask = new Runnable() {
  public void run() {
    //run X activity
    firstClick = true;
  }
};
boolean firstClick = true;

(and then in onClick)

if (firstClick) {
  handler.postDelayed(singleClickTask, 500);
  firstClick=false;
} else {
  handler.removeCallbacks(singleClickTask);
  firstClick=true;
  //run Y activity
}

Please debug this yourself before using it blindly, but I think it's right.

Upvotes: 1

Related Questions