jeudi 7 juillet 2016

How can I catch the volume button's events of headset, even the phone screen is locked?

I want to do something in my app by combining volume buttons and pause button of the headset.

It's easy to catch the pause button's events by registering a mediaButtonReceiver with IntentFilter(Intent.Action_MEDIA_BUTTON) in the service I created.

But it can't catch the other two buttons' events.

I've tried catching 'android.media.VOLUME_CHANGED_ACTION' and I think there will be a event I could get. But I was told (Attempt to invoke virtual method 'int android.view.KeyEvent.getKeyCode()' on a null object reference) as my code is

KeyEvent keyEvent = (KeyEvent) intent .getParcelableExtra(Intent.EXTRA_KEY_EVENT); int keyCode=keyEvent.getKeyCode();.

It seems like the android.media.VOLUME_CHANGED_ACTION does not carry a Intent.EXTRA_KEY_EVENT.

So, is there any way to catch the volume button's events of headset? And how can I use the APIs?

Thanks a lot :).

Aucun commentaire:

Enregistrer un commentaire