title: Headset button double click in android
body: <p>I used this code for detecting single and double click for headset button in my broadcast receiver:</p>
  
  <pre><code>int d = 0;
  @Override
  public void onReceive(Context context, Intent intent) {
      String intentAction = intent.getAction();
      if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
          return;
      }
      KeyEvent event = (KeyEvent) intent
              .getParcelableExtra(Intent.EXTRA_KEY_EVENT);
      if (event == null) {
          return;
      }
      int action = event.getAction();
      switch (event.getKeyCode()) {
          case KeyEvent.KEYCODE_HEADSETHOOK:
              if (action == KeyEvent.ACTION_DOWN) {
                  d++;
                  Handler handler = new Handler();
                  Runnable r = new Runnable() {                   
                      @Override
                      public void run() {
                          Toast.makeText(context, "single click!", Toast.LENGTH_SHORT).show();
                          d = 0;
                      }
                  };
                  if (d == 1) {
                      handler.postDelayed(r, 500);
                  } else if (d == 2) {
                      d = 0;
                      Toast.makeText(context, "double click!", Toast.LENGTH_SHORT).show();
                  }
              }break;
      }
      abortBroadcast();
  
  }
  </code></pre>
  
  <p>but it just detects single click. Two single click instead of double click. Where's the problem?</p>