A media button receiver receives and helps translate hardware media playback buttons, such as those found on wired and wireless headsets, into the appropriate callbacks in your app.
You can add this MediaButtonReceiver to your app by adding it directly to your AndroidManifest.xml:
This class assumes you have a Service in your app that controls media playback via a MediaSessionCompat. Once a key event is received by MediaButtonReceiver, this class tries to find a Service that can handle ACTION_MEDIA_BUTTON, and a MediaBrowserServiceCompat in turn. If an appropriate service is found, this class forwards the key event to the service. If neither is available or more than one valid service/media browser service is found, an IllegalStateException will be thrown. Thus, your app should have one of the following services to get a key event properly.
Service Handling ACTION_MEDIA_BUTTON
A service can receive a key event by including an intent filter that handles ACTION_MEDIA_BUTTON:
Creates a broadcast pending intent that will send a media button event. The action will be translated to the appropriate KeyEvent, and sent to the provided media button receiver via the pending intent. The action should be one of the following:
Creates a broadcast pending intent that will send a media button event. The action will be translated to the appropriate KeyEvent, and it will be sent to the registered media button receiver in the given context. The action should be one of the following:
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]