Geva Tal Geva Tal - 1 month ago 5x
Android Question

MEDIA_BUTTON button event ACTION_DOWN and ACTION_UP received at the same time

In my app I want to measure how long the media button press was. I registered a broadcastReceiver that listens to the media button press: (please excuse stupid mistakes as I am a beginner...)

<receiver android:name="MyRec">
<action android:name="android.intent.action.MEDIA_BUTTON">
<action android:name="android.intent.action.MEDIA_BUTTON"/>

The broadcastReceiver activates a method in an activity (not ideal, but this is just for testing purposes):

public void onReceive(Context context, Intent intent) {
KeyEvent Xevent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
MainActivity inst = MainActivity.instance();
if ((Xevent.getAction() == KeyEvent.ACTION_DOWN)){
else if ((Xevent.getAction() == KeyEvent.ACTION_UP)) {

The activity takes system time when startTimer() is called and then again when stopTimer is called and shows the diff:

public void startTimer(){
pressTime = System.currentTimeMillis();

public void stopTimer(){
pressDuration = System.currentTimeMillis() - pressTime;
TextView timerTextView = (TextView)findViewById(;

The issue is that from what I see both events are called at the same time, both when I let go of the button, what eventually makes the timer count a very short time period (few millisecond) that are not related to the duration I press the button.

What am I doing wrong?


You don't need to use your own timers. You can use the getDownTime and getEventTime methods of the event parameter when receiving the KeyEvent.ACTION_UP action.

Also, The nested <action android:name="android.intent.action.MEDIA_BUTTON"/> in your manifest is not required