How to detect headset button double click in HTC phones with broadcast receiver?

浪尽此生 提交于 2019-12-11 02:18:18

问题


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?


回答1:


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>



回答2:


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

            }
        }


来源:https://stackoverflow.com/questions/20718617/how-to-detect-headset-button-double-click-in-htc-phones-with-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!