Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture press and hold (long press) of the ACTION_MEDIA_BUTTON

The keydown documentation states that the keydown will be sent again if it's held down, but if you hold down the media key I don't get any notification at all. Is there a way to detect this? It's actually going to a Google (Now?) Search voice-input - perhaps there's a broadcast indicating that is happening I could receive?

Here's my BroadcastReceiver's receiver:

@Override
public void onReceive(Context context, Intent intent)
{
    String intentAction = intent.getAction();
    int SoundID = 0;

    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction))
    {
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event != null)
        {               
            int action = event.getAction();

            if (action == KeyEvent.ACTION_DOWN)
            {
                Log.d("Hi", "MEDIA keydown r" + event.getRepeatCount());

                if (event.getRepeatCount() == 1)
                    DoSomethingMagical();
                else if (event.getRepeatCount() == 0)
                    DoSomethingSpecial();
            }
        }
    }
}
like image 324
noelicus Avatar asked Jan 31 '26 13:01

noelicus


1 Answers

You can react on the long press event by adding the following intent-filter to your AndroidManifest.xml:

<intent-filter>
  <action android:name="android.speech.action.WEB_SEARCH"/>
  <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

Unfortunately this will not be handled in the same way as an action of type android.intent.action.MEDIA_BUTTON (your BroadcastReceiver won't be invoked).

like image 138
einsA Avatar answered Feb 02 '26 01:02

einsA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!