How to use the Headphone button as push to talk button in android
<p>I want to use the Headphone button "KEYCODE_HEADSETHOOK" as push to talk button in android, ie when press and hold i can do something then stop when I release it.
Thanks</p>

<p>here is my code </p>

<pre><code>if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK/*KEYCODE_HEADSETHOOK*/ &amp;&amp; event.getAction() == KeyEvent.ACTION_DOWN) {
     Log.e("RAM","Set");
     setPushToTalk(true);
     return true;
     }
    else if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK &amp;&amp; event.getRepeatCount() == 0 &amp;&amp; (event.getFlags() &amp; 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;
        }
</code></pre>
