Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,811,847 Members 53,505 Now Online
XDA Developers Android and Mobile Development Forum

keycode_headsethook "not working"

Tip us?
 
dkovacev
Old
#1  
Junior Member - OP
Thanks Meter 0
Posts: 5
Join Date: Dec 2008
Default keycode_headsethook "not working"

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
Old
#2  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 325
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by dkovacev View Post
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
Old
#3  
Junior Member - OP
Thanks Meter 0
Posts: 5
Join Date: Dec 2008
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
Old
#4  
SimplicityApks's Avatar
Senior Member
Thanks Meter 327
Posts: 325
Join Date: May 2013
Location: Aachen
Quote:
Originally Posted by dkovacev View Post
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.
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes