android – Long press on volume buttons

Question:

I'm doing something like an mp3 player. It is necessary, with the keyboard locked, to track a long press on the volume buttons, one hundred would switch the music. How can this be done?

Answer:

You will have to catch the time of pressing the volume button yourself in milliseconds!

Tracking algorithm:

The button was pressed for the first time!

(1) set the flag of the first press (2) got the time.

The button was pressed for more than (for example) 2000 ms, go ahead!

The button was released earlier than 2000 ms: (1) clear the flag of the first press.

Use: onTouch (View v, MotionEvent event) {}

Scroll to Top