Android Question Reading the headphone button

jbellora

Member
Licensed User
Hello,

using Nexus 7 (first model) and

Sub Activity_Keypress(keycode As Int) As Boolean
ToastMessageShow(keycode,False)
Return True

End Sub

I can see keycodes 24 and 25 for Volume Up and Down, but don't see anything when pressing the headphone button.
Do I need to add something to the Manifest?

Thanks for your help.
 

jbellora

Member
Licensed User
Yes. That are the keycodes constants to be returned. Just integers. But Activity_Keypress does not fire with the Headphone Button.
Should return 79, I believe.
 

NJDude

Expert
Licensed User
You asked for something similar before if I'm not mistaken, if your intention is to detect if the headphone is plugged/unplugged then you should use BroadcastReceiver.
 

jbellora

Member
Licensed User
True. But I read that Activity_Keypress could read the Headphone button.
I don't need to know if it is plugged. Just read the button, will assume it plugged.

Tried to use BroadcastReceiver, with no success.
Also a Reflector, but I don't know what target to use:

in Globals:
Dim r As Reflector

in Activity_Create:
r.Target=Activity 'with Activity, it does not work. What target to use?
r.SetOnKeyListener("Key_input")

and the Sub
Sub Key_input(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
ToastMessageShow(KeyCode,False)
Return True
End Sub

Or with Reflector (that seemed simple) it's impossible?

Thank you, NJ Dude!
 

NJDude

Expert
Licensed User
I don't need to know if it is plugged. Just read the button, will assume it plugged.
Not quite understand what you mean by that but look at the attached sample, it uses the BroadcastReceiver library and I can detect when the headphone is plugged or unplugged.
 

Attachments

NJDude

Expert
Licensed User
If you mean by "abort" you seeing a black screen, then it is working, have you plugged the headphone? you will see a Toast showing the action (plugged or unplugged).
 

jbellora

Member
Licensed User
No, I added a label in the main activity, just to see something. And in Activity_Resume I put some text.
Everything shows (very quickly) and then I get a message (in spanish) that translated says: "Sorry, the application BroadcastReceiver Sample has stopped"
 

NJDude

Expert
Licensed User
Without seeing what you've done I have no clue what the problem could be, however, the sample as I posted it works, I would suggest you try my original code first.
 

jbellora

Member
Licensed User
I first used your original code, as you sent it.
What I found now is that if instead of android.intent.action.HEADSET_PLUG I use another intent like android.intent.action.MEDIA_BUTTON the app does not stop but, of couse, does not do what we want.
Perhaps the Android version has something to do. I'm using Android 4.3 JWR6&Y build.
 
Top