Botón de auricular doble clic en Android

Utilicé este código para detectar un clic y un doble clic en el botón del auricular en mi receptor de difusión:

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();

}

pero simplemente detecta un solo clic. Dos clic en lugar de doble clic. ¿Dónde está el problema?

Respuestas a la pregunta(1)

Su respuesta a la pregunta