Android Question Reading the headphone button

jbellora

Member
Licensed User
Longtime 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
Longtime 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.
 
Upvote 0

jbellora

Member
Licensed User
Longtime 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!
 
Upvote 0

NJDude

Expert
Licensed User
Longtime 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

  • BroadcastReceiverSample.zip
    6.3 KB · Views: 355
Upvote 0

jbellora

Member
Licensed User
Longtime 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"
 
Upvote 0

jbellora

Member
Licensed User
Longtime 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.
 
Upvote 0
Top