4

I am building an Android app, which detects the selfie-stick button click connected to the mobile audio jack.

I am able to capture the button click if the app is in the foreground, but not in the background.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
   switch (keyCode) {
       case KeyEvent.KEYCODE_VOLUME_UP:
           Toast.makeText(CustomPadActivity.this, "BUTTON clicked", Toast.LENGTH_SHORT).show();
           return true;
   }
   return super.onKeyDown(keyCode, event);
}

I tried using the broadcast receiver with action android.intent.action.MEDIA_BUTTON but it is not capturing the button click at all.

Manifest:

<receiver android:name=".MyReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

And my Receiver class:

public class MyReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "BUTTON clicked", Toast.LENGTH_SHORT).show();
    }

}

I read somewhere I need to play a piece of music if I want to capture the button click. The app is not an audio player. I don't want to play audio in the app.

I am just trying to capture the selfie-stick button click when my app is in the background.

Twisha Kotecha
  • 1,082
  • 1
  • 4
  • 18
Dileep Perla
  • 1,865
  • 7
  • 33
  • 54

1 Answers1

1

This is of course

onKeyEvent only can be callbacks in the activity

Services simply do not receive KeyEvent callbacks.

Add intent to AndroidManifest in Activtiy

<intent-filter>
    <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>

and add this in your Activity or it will not show on your lockscreen.

@Override
public void onAttachedToWindow() {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
lazy
  • 149
  • 8
  • How "android.intent.action.MEDIA_BUTTON" intent-filter in Activity will actually help me to capture when the app is in the background? – Dileep Perla Mar 06 '20 at 06:13
  • I was wrong, it really should be in the receiver, you can do it in onReceive if(intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) // do something – lazy Mar 06 '20 at 06:30
  • Done that already, didn't work, mentioned the same in my question too. – Dileep Perla Mar 06 '20 at 06:32
  • Have you tried registering BroadcastReceiver with Service? Then capture it with onReceive too. RegisterReceiver (myReceiver, new IntentFilter ("android.media.VOLUME_CHANGED_ACTION")); – lazy Mar 06 '20 at 06:50
  • This may help you https://stackoverflow.com/a/44040282/11732119 – lazy Mar 06 '20 at 06:51
  • 1
    Hey, I tried with the link you have provided, It is kind of working partially. Whatever the priority I set, android OS is taking the first priority when I click the stick button, but if I click the button twice immediately, then my app is able to capture the second click. – Dileep Perla Mar 09 '20 at 07:26