keycode_headsethook "not working"

Search This thread

dkovacev

Member
Dec 18, 2008
6
0
www.oserk.com
Hi!

I've made a simple app that needs to catch mic button click from wired headset, and I tried to implement that via simple onKeyDown function in activity, and also with broadcastreciever, but app doesn't react on click. Or to be precise it doesn't react on some generic android phone with android 4.2, while working perfecty fine on ZTE Blade III. It does however react on other keys like Volume Up/Down. I was thinkg that maybe builtin apps have even greater priorty then my app, but don't know hot to check that, and more importantly how to prevent that, (priority of 100000 didin't help) . Also to mention that phone is registering button for recieve call, so it doesn't look like it's a hw problem.
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Hi!

I've made a simple app that needs to catch mic button click from wired headset, and I tried to implement that via simple onKeyDown function in activity, and also with broadcastreciever, but app doesn't react on click. Or to be precise it doesn't react on some generic android phone with android 4.2, while working perfecty fine on ZTE Blade III. It does however react on other keys like Volume Up/Down. I was thinkg that maybe builtin apps have even greater priorty then my app, but don't know hot to check that, and more importantly how to prevent that, (priority of 100000 didin't help) . Also to mention that phone is registering button for recieve call, so it doesn't look like it's a hw problem.

Could you show us your onKeyDown implementation? That would certainly help. I'm guessing you have a certain keyCode you are checking in that method, it could be as simple as that the 4.2 device just has it configured differently so the keycode passed to onKeyDown is different. Just put a Log.d("myApp", "Pressed key with code: "+keycode); in there and see if you get any logs.

If the whole method isn't called, it might be that some view in the activity is consuming the key event, as indicated in the documantation. It is also possible that the onKeyDown isn't called at all for you keycode.
 

dkovacev

Member
Dec 18, 2008
6
0
www.oserk.com
Thanks for the interest, code is simple

Code:
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	Toast.makeText( this, "BUTTON pressed!", Toast.LENGTH_SHORT).show(); 
    	
    	String myStr = ""; 
        if ((keyCode == KeyEvent.ACTION_DOWN)){
        	Toast.makeText( this, "BUTTON down", Toast.LENGTH_SHORT).show(); 
        }

        if ((keyCode == KeyEvent.KEYCODE_HEADSETHOOK)){
        	Toast.makeText( this, "BUTTON Headset key Down!", Toast.LENGTH_SHORT).show(); 
        }

        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        	Toast.makeText( this, "BUTTON volume Down!", Toast.LENGTH_SHORT).show(); 
        }
        ...

As you can guess, nothing happens even before 'if statements', VOLUME_UP works fine, same thing happens in onKeyUp function.
I also tried with broadcast recievers, copy/pasted sample from SDK (Random Music Player) that works with Broadcast Recievers, still nothing.

Button itself, works for recieving calls, but nowhere else, I read somewhere that with JellyBean Google introduced Media control with mic button for play/pause functionality in music player, but it won't work for me, when I press buttton during playback, volume is up, while holding, and that's it.

And still, same code works perfectly fine on 4.0.4, maybe it's something with this particular 4.2.2 that prevents me from capturing key, but don't know how and where to look about it
 

SimplicityApks

Senior Member
May 26, 2013
354
344
Aachen
Thanks for the interest, code is simple

Code:
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	
    	Toast.makeText( this, "BUTTON pressed!", Toast.LENGTH_SHORT).show(); 
    	
    	String myStr = ""; 
        if ((keyCode == KeyEvent.ACTION_DOWN)){
        	Toast.makeText( this, "BUTTON down", Toast.LENGTH_SHORT).show(); 
        }

        if ((keyCode == KeyEvent.KEYCODE_HEADSETHOOK)){
        	Toast.makeText( this, "BUTTON Headset key Down!", Toast.LENGTH_SHORT).show(); 
        }

        if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){
        	Toast.makeText( this, "BUTTON volume Down!", Toast.LENGTH_SHORT).show(); 
        }
        ...

As you can guess, nothing happens even before 'if statements', VOLUME_UP works fine, same thing happens in onKeyUp function.
I also tried with broadcast recievers, copy/pasted sample from SDK (Random Music Player) that works with Broadcast Recievers, still nothing.

Button itself, works for recieving calls, but nowhere else, I read somewhere that with JellyBean Google introduced Media control with mic button for play/pause functionality in music player, but it won't work for me, when I press buttton during playback, volume is up, while holding, and that's it.

And still, same code works perfectly fine on 4.0.4, maybe it's something with this particular 4.2.2 that prevents me from capturing key, but don't know how and where to look about it

Ah ok, so it doesn't even get called :/ Then I would probably try to use the broadcastReceiver again, did you do it like this (its a different question but the code looks fine)? I think it's better to use a receiver anyway since it is not connected to the activity or the views in it.