Question

I'm developing a headset button controller and use a broadcast receiver for detecting headset button presses.

((AudioManager) getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName()));

onReceive method:

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) {
            long time = SystemClock.uptimeMillis();
            // double click
            if (time - sLastClickTime < DOUBLE_CLICK_DELAY)
                // do something
                Toast.makeText(context, "BUTTON PRESSED DOUBLE!",
                        Toast.LENGTH_SHORT).show();
            // single click
            else {
                // do something
                Toast.makeText(context, "BUTTON PRESSED!",
                        Toast.LENGTH_SHORT).show();
            }
            sLastClickTime = time;
        }
        break;
    }
    abortBroadcast();

}

and it works fine. The problem is in HTC android phones It can't receive double click. When you double click headset button it dials your last call by default and I can't detect double click in my app. Is there any way to disable this action with android APIs?

I tried setting my broadcast receiver's priority to a large number and it didn't work. Even I tried to placing a fake/invalid call in phone's call log but I couldn't do that. Any Idea how can I solve this problem?

Was it helpful?

Solution

The solution was so simple. Setting priority to largest integer number (2147483647) instead of largest value defined by Google (1000), in manifest file solves the problem, and phone doesn't dial last number by double clicking and broadcast receiver detects the headset button double click.

<receiver
        android:name=".MediaButtonIntentReceiver"
        android:enabled="true" >
        <intent-filter android:priority="2147483647">
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
</receiver>

OTHER TIPS

Your solution didn't work for me also it is not best practice.

For others who are having same problem. This is what I used in my music player to handle headset control single and double click. This should work for all headsets.

static final long CLICK_DELAY = 500;
static long lastClick = 0; // oldValue
static long currentClick = System.currentTimeMillis();

@Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) {

            KeyEvent keyEvent = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);

            if (keyEvent.getAction() != KeyEvent.ACTION_DOWN)return;

            lastClick = currentClick ;

            currentClick = System.currentTimeMillis();

            if(currentClick - lastClick < CLICK_DELAY ){

                 //This is double click

            } else { 

                 //This is single click

            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top