0

我想在android中使用耳机按钮“KEYCODE_HEADSETHOOK”作为通话按钮,即按住时我可以做某事,然后当我释放它时停止。谢谢

这是我的代码

if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK/*KEYCODE_HEADSETHOOK*/ && event.getAction() == KeyEvent.ACTION_DOWN) {
     Log.e("RAM","Set");
     setPushToTalk(true);
     return true;
     }
    else if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK && event.getRepeatCount() == 0 && (event.getFlags() & KeyEvent.FLAG_LONG_PRESS) == KeyEvent.FLAG_LONG_PRESS) {
        switch (keyCode) {
        case MotionEvent.ACTION_DOWN:
            if(!mService.isConnected()){
                playTone();
                return false ;
            }
            boolean waitFlag = false;
            for (User user : mService.getUserList()) {
                if (user.getChannel().equals(getChannel())) {
                    if (user.talkingState == 1)
                        waitFlag = true;// user);
                    // Log.e("Talking user",user.name+"/"+user.talkingState);
                }
            }
            if (waitFlag){
                sendWaitingMsg();
            }
            else{
                setPushToTalk(true);
            }
            Log.e("headphone btn", "down");
            return true;
        case MotionEvent.ACTION_UP:
            setPushToTalk(false);
            Log.e("headphone btn", "up");
            return true;
        }
4

1 回答 1

1

看着http://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_HEADSETHOOK

它看起来像一个单一的标志。它只会在按下时发送一条消息,按住不会产生稳定的输入,您必须使用按钮“切换”通话,如果标志被它打开/关闭,也许允许语音检测功能或避免keyEvent 完全和做硬件寻址?

于 2013-04-02T11:47:25.583 回答