Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Headset button double click in android

I used this code for detecting single and double click for headset button in my broadcast receiver:

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

}

but it just detects single click. Two single click instead of double click. Where's the problem?

like image 595
Mehrdad Avatar asked Dec 23 '13 21:12

Mehrdad


People also ask

How to detect double tap in Android Studio?

Step 1: Create an Empty activity in Android Studio. To create one, follow this article- How to Create/Start a New Project in Android Studio?. Check if the primary language selected is Kotlin. Step 2: In activity_main.xml, add a button which will detect the double tap.

How to create a double tap button in Kotlin?

Check if the primary language selected is Kotlin. Step 2: In activity_main.xml, add a button which will detect the double tap. Step 3: In this step add the abstract class for the double tap and set the onClickListener which will use the abstract class. and show the toast.

Will Google bring back the double tap gesture on the pixel?

With Tap, Tap, you don't need to wait for Google to bring back the Pixel's double-tap gesture. If you’ve been following Google’s work on Android 11, you’ll likely be familiar with the back tap gesture the company had included in its early developer previews.

What is the Android 11 double-tap gesture?

The Android 11 double-tap gesture allowed you to tap the back of your Pixel device to do things like launch the phone’s camera app and control media playback. Google further built out the gesture in DP2 but removed it with subsequent betas.


1 Answers

The correct solution:

static 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() {
                    // single click *******************************
                    if (d == 1) {
                                Toast.makeText(context, "single click!", Toast.LENGTH_SHORT).show();
                    }
                    // double click *********************************
                    if (d == 2) {
                        Toast.makeText(context, "Double click!!", Toast.LENGTH_SHORT).show();
                    }
                    d = 0;
            }
            };
            if (d == 1) {
                    handler.postDelayed(r, 500);
            }
        }break;
   }
   abortBroadcast();
}
like image 88
Mehrdad Avatar answered Oct 06 '22 01:10

Mehrdad