# MediaButtonReceiver

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:support-media-compat:28.0.0-alpha1  
Summary:[Ctors](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#)  

# MediaButtonReceiver

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


class
MediaButtonReceiver
`  
`


``

extends `[BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html)`


``


`

|---|---|------------------------------------------------------|
| java.lang.Object                                           |||
| ↳ | [android.content.BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html) ||
|   | ↳ | android.support.v4.media.session.MediaButtonReceiver |

<br />

*** ** * ** ***

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.

<br />

You can add this MediaButtonReceiver to your app by adding it directly to your AndroidManifest.xml:  

```
 <receiver android:name="android.support.v4.media.session.MediaButtonReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </receiver>
 
```
This class assumes you have a[Service](https://developer.android.com/reference/android/app/Service.html)in your app that controls media playback via a[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat). Once a key event is received by MediaButtonReceiver, this class tries to find a[Service](https://developer.android.com/reference/android/app/Service.html)that can handle[ACTION_MEDIA_BUTTON](https://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON), and a[MediaBrowserServiceCompat](https://developer.android.com/reference/android/support/v4/media/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](https://developer.android.com/reference/java/lang/IllegalStateException)will be thrown. Thus, your app should have one of the following services to get a key event properly.

<br />

#### Service Handling ACTION_MEDIA_BUTTON

A service can receive a key event by including an intent filter that handles[ACTION_MEDIA_BUTTON](https://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON):  

```
 <service android:name="com.example.android.MediaPlaybackService" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </service>
 
```
Events can then be handled in[onStartCommand(Intent, int, int)](https://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int))by calling[handleIntent(MediaSessionCompat, Intent)](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#handleIntent(android.support.v4.media.session.MediaSessionCompat, android.content.Intent)), passing in your current[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat):  

```
 private MediaSessionCompat mMediaSessionCompat = ...;

 public int onStartCommand(Intent intent, int flags, int startId) {
   MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent);
   return super.onStartCommand(intent, flags, startId);
 }
 
```
This ensures that the correct callbacks to[MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback)will be triggered based on the incoming[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html).

**Note:**Once the service is started, it must start to run in the foreground.

#### MediaBrowserService

If you already have a[MediaBrowserServiceCompat](https://developer.android.com/reference/android/support/v4/media/MediaBrowserServiceCompat)in your app, MediaButtonReceiver will deliver the received key events to the[MediaBrowserServiceCompat](https://developer.android.com/reference/android/support/v4/media/MediaBrowserServiceCompat)by default. You can handle them in your[MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback).

<br />

## Summary

|                                                               ### Public constructors                                                                ||
|---------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[MediaButtonReceiver](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#MediaButtonReceiver())`() ` |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ### Public methods                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ||
|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` static `[PendingIntent](https://developer.android.com/reference/android/app/PendingIntent.html) | ` `[buildMediaButtonPendingIntent](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#buildMediaButtonPendingIntent(android.content.Context, android.content.ComponentName, long))`(`[Context](https://developer.android.com/reference/android/content/Context.html)` context, `[ComponentName](https://developer.android.com/reference/android/content/ComponentName.html)` mbrComponent, long action) ` Creates a broadcast pending intent that will send a media button event.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| ` static `[PendingIntent](https://developer.android.com/reference/android/app/PendingIntent.html) | ` `[buildMediaButtonPendingIntent](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#buildMediaButtonPendingIntent(android.content.Context, long))`(`[Context](https://developer.android.com/reference/android/content/Context.html)` context, long action) ` Creates a broadcast pending intent that will send a media button event.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| ` static `[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)          | ` `[handleIntent](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#handleIntent(android.support.v4.media.session.MediaSessionCompat, android.content.Intent))`(`[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat)` mediaSessionCompat, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` intent) ` Extracts any available[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)from an[ACTION_MEDIA_BUTTON](https://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON)intent, passing it onto the[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat)using[dispatchMediaButtonEvent(KeyEvent)](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat#dispatchMediaButtonEvent(android.view.KeyEvent)), which in turn will trigger callbacks to the[MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback)registered via[setCallback(MediaSessionCompat.Callback)](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat#setCallback(android.support.v4.media.session.MediaSessionCompat.Callback)). |
| ` void`                                                                                           | ` `[onReceive](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver#onReceive(android.content.Context, android.content.Intent))`(`[Context](https://developer.android.com/reference/android/content/Context.html)` context, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` intent) `                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#)From class` `[android.content.BroadcastReceiver](https://developer.android.com/reference/android/content/BroadcastReceiver.html)` ` |------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ` final void`                                                                                                                            | ` abortBroadcast() `                                                                                                                                                                          | | ` final void`                                                                                                                            | ` clearAbortBroadcast() `                                                                                                                                                                     | | ` final boolean`                                                                                                                         | ` getAbortBroadcast() `                                                                                                                                                                       | | ` final boolean`                                                                                                                         | ` getDebugUnregister() `                                                                                                                                                                      | | ` final int`                                                                                                                             | ` getResultCode() `                                                                                                                                                                           | | ` final String`                                                                                                                          | ` getResultData() `                                                                                                                                                                           | | ` final `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)                                                        | ` getResultExtras(boolean arg0) `                                                                                                                                                             | | ` final `[BroadcastReceiver.PendingResult](https://developer.android.com/reference/android/content/BroadcastReceiver.PendingResult.html) | ` goAsync() `                                                                                                                                                                                 | | ` final boolean`                                                                                                                         | ` isInitialStickyBroadcast() `                                                                                                                                                                | | ` final boolean`                                                                                                                         | ` isOrderedBroadcast() `                                                                                                                                                                      | | ` abstract void`                                                                                                                         | ` onReceive(`[Context](https://developer.android.com/reference/android/content/Context.html)` arg0, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` arg1) `   | | ` `[IBinder](https://developer.android.com/reference/android/os/IBinder.html)                                                            | ` peekService(`[Context](https://developer.android.com/reference/android/content/Context.html)` arg0, `[Intent](https://developer.android.com/reference/android/content/Intent.html)` arg1) ` | | ` final void`                                                                                                                            | ` setDebugUnregister(boolean arg0) `                                                                                                                                                          | | ` final void`                                                                                                                            | ` setOrderedHint(boolean arg0) `                                                                                                                                                              | | ` final void`                                                                                                                            | ` setResult(int arg0, String arg1, `[Bundle](https://developer.android.com/reference/android/os/Bundle.html)` arg2) `                                                                         | | ` final void`                                                                                                                            | ` setResultCode(int arg0) `                                                                                                                                                                   | | ` final void`                                                                                                                            | ` setResultData(String arg0) `                                                                                                                                                                | | ` final void`                                                                                                                            | ` setResultExtras(`[Bundle](https://developer.android.com/reference/android/os/Bundle.html)` arg0) `                                                                                          | ||
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v4-media-session/MediaButtonReceiver#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### MediaButtonReceiver

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
MediaButtonReceiver ()
```

<br />

<br />

## Public methods

### buildMediaButtonPendingIntent

added in[version 25.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
PendingIntent buildMediaButtonPendingIntent (Context context,
                ComponentName mbrComponent,
                long action)
```

Creates a broadcast pending intent that will send a media button event. The`action`will be translated to the appropriate[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html), and sent to the provided media button receiver via the pending intent. The`action`should be one of the following:

- [ACTION_PLAY](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PLAY)
- [ACTION_PAUSE](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PAUSE)
- [ACTION_SKIP_TO_NEXT](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_SKIP_TO_NEXT)
- [ACTION_SKIP_TO_PREVIOUS](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_SKIP_TO_PREVIOUS)
- [ACTION_STOP](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_STOP)
- [ACTION_FAST_FORWARD](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_FAST_FORWARD)
- [ACTION_REWIND](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_REWIND)
- [ACTION_PLAY_PAUSE](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PLAY_PAUSE)

<br />

<br />

|                                                           Parameters                                                           ||
|----------------|----------------------------------------------------------------------------------------------------------------|
| `context`      | `Context`: The context of the application. <br />                                                              |
| `mbrComponent` | `ComponentName`: The full component name of a media button receiver where you want to send this intent. <br /> |
| `action`       | `long`: The action to be sent via the pending intent. <br />                                                   |

|                                                                                                  Returns                                                                                                  ||
|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------|
| [PendingIntent](https://developer.android.com/reference/android/app/PendingIntent.html) | Created pending intent, or null if the given component name is null or the`action`is unsupported/invalid. <br /> |

### buildMediaButtonPendingIntent

added in[version 25.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
PendingIntent buildMediaButtonPendingIntent (Context context,
                long action)
```

Creates a broadcast pending intent that will send a media button event. The`action`will be translated to the appropriate[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html), and it will be sent to the registered media button receiver in the given context. The`action`should be one of the following:

- [ACTION_PLAY](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PLAY)
- [ACTION_PAUSE](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PAUSE)
- [ACTION_SKIP_TO_NEXT](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_SKIP_TO_NEXT)
- [ACTION_SKIP_TO_PREVIOUS](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_SKIP_TO_PREVIOUS)
- [ACTION_STOP](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_STOP)
- [ACTION_FAST_FORWARD](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_FAST_FORWARD)
- [ACTION_REWIND](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_REWIND)
- [ACTION_PLAY_PAUSE](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat#ACTION_PLAY_PAUSE)

<br />

<br />

|                               Parameters                                ||
|-----------|--------------------------------------------------------------|
| `context` | `Context`: The context of the application. <br />            |
| `action`  | `long`: The action to be sent via the pending intent. <br /> |

|                                                                                                              Returns                                                                                                              ||
|-----------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------|
| [PendingIntent](https://developer.android.com/reference/android/app/PendingIntent.html) | Created pending intent, or null if cannot find a unique registered media button receiver or if the`action`is unsupported/invalid. <br /> |

### handleIntent

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
KeyEvent handleIntent (MediaSessionCompat mediaSessionCompat,
                Intent intent)
```

Extracts any available[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)from an[ACTION_MEDIA_BUTTON](https://developer.android.com/reference/android/content/Intent.html#ACTION_MEDIA_BUTTON)intent, passing it onto the[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat)using[dispatchMediaButtonEvent(KeyEvent)](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat#dispatchMediaButtonEvent(android.view.KeyEvent)), which in turn will trigger callbacks to the[MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback)registered via[setCallback(MediaSessionCompat.Callback)](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat#setCallback(android.support.v4.media.session.MediaSessionCompat.Callback)).

<br />

|                                                                                                                                                       Parameters                                                                                                                                                       ||
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `mediaSessionCompat` | `MediaSessionCompat`: A[MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat)that has a[MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback)set. <br /> |
| `intent`             | `Intent`: The intent to parse. <br />                                                                                                                                                                                                                                                            |

|                                                                                               Returns                                                                                                ||
|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------|
| [KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html) | The extracted[KeyEvent](https://developer.android.com/reference/android/view/KeyEvent.html)if found, or null. <br /> |

### onReceive

added in[version 24.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void onReceive (Context context,
                Intent intent)
```

<br />

<br />

|         Parameters          ||
|-----------|------------------|
| `context` | `Context` <br /> |
| `intent`  | `Intent` <br />  |

-

  ## Interfaces

  - [MediaSessionCompat.OnActiveChangeListener](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.OnActiveChangeListener)
-

  ## Classes

  - [MediaButtonReceiver](https://developer.android.com/reference/android/support/v4/media/session/MediaButtonReceiver)
  - [MediaControllerCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat)
  - [MediaControllerCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat.Callback)
  - [MediaControllerCompat.PlaybackInfo](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat.PlaybackInfo)
  - [MediaControllerCompat.TransportControls](https://developer.android.com/reference/android/support/v4/media/session/MediaControllerCompat.TransportControls)
  - [MediaSessionCompat](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat)
  - [MediaSessionCompat.Callback](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Callback)
  - [MediaSessionCompat.QueueItem](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.QueueItem)
  - [MediaSessionCompat.Token](https://developer.android.com/reference/android/support/v4/media/session/MediaSessionCompat.Token)
  - [ParcelableVolumeInfo](https://developer.android.com/reference/android/support/v4/media/session/ParcelableVolumeInfo)
  - [PlaybackStateCompat](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat)
  - [PlaybackStateCompat.Builder](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.Builder)
  - [PlaybackStateCompat.CustomAction](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.CustomAction)
  - [PlaybackStateCompat.CustomAction.Builder](https://developer.android.com/reference/android/support/v4/media/session/PlaybackStateCompat.CustomAction.Builder)