I tried out different variants of the event interception code.
Intent.ACTION_MEDIA_BUTTON)
for wired headset buttons,
for example, case KeyEvent.KEYCODE_MEDIA_NEXT:
on the link one of examples from Google RandomMusicPlayer

On 4.4, everything works as expected in the application log, I see that onReceive works as it should, but Android 5.1 intercepts the events of the standard system volume control.

  • What is the priority of Receiver ? - Flippy
  • The priority was set by <intent-filter android: priority = "2147483647"> <action android: name = "android.intent.action.MEDIA_BUTTON" /> according to the documentation, the maximum 1000 also tried it. - x555xx

1 answer 1

Recently, the MEDIA_BUTTON handler wrote, also encountered a problem when the receiver triggered on 4.4 and did not work on 5.1, I found this solution:

 public final class MediaButtonMonitorService extends Service { /** * Компонент обработки кнопок гарнитуры. */ private ComponentName mMediaButtonReceiver = null; /** * Сессия. */ private MediaSessionCompat mMediaSession = null; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Log.d("Create mediaButtonListener"); // Создаем объекты для обработки кнопок гарнитуры mMediaButtonReceiver = new ComponentName(this, MediaButtonReceiver.class); mMediaSession = new MediaSessionCompat(this, "TAG", mMediaButtonReceiver, /* pendingIntent */ null); mMediaSession.setCallback(new MediaSessionCompat.Callback() { @Override public void onCustomAction(String action, Bundle extras) { super.onCustomAction(action, extras); } @Override public boolean onMediaButtonEvent(Intent mediaButtonEvent) { if (Intent.ACTION_MEDIA_BUTTON.equals(mediaButtonEvent.getAction())) { final KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); if (KeyEvent.ACTION_DOWN == keyEvent.getAction()) { Log.v( "Media button down!"); } } else { Log.v( "Unknown intent: " + mediaButtonEvent.getAction()); } return true; } }); mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS); mMediaSession.setActive(true); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.v("Start mediaButtonListener"); return START_STICKY; } @Override public void onDestroy() { Log.v("Finish mediaButtonListener"); super.onDestroy(); } } 

It turned out that on 4.4 I had a normal receiver , and on 5.1 this handler worked.

  • Thanks, I will understand. - x555xx
  • By your example, I found a full-fledged example on github . - x555xx