public
class
MediaButtonReceiver
extends BroadcastReceiver
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:
<receiver android:name="androidx.media.session.MediaButtonReceiver" >
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
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
Intent.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.
A service can receive a key event by including an intent filter that handles
Intent.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
Service.onStartCommand(Intent, int, int)
by calling
handleIntent(MediaSessionCompat, Intent)
, passing in your current
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
will be triggered
based on the incoming
KeyEvent
.
Note: Once the service is started, it must start to run in the
foreground.
If you already have a
MediaBrowserServiceCompat
in your app, MediaButtonReceiver will
deliver the received key events to the
MediaBrowserServiceCompat
by default. You can
handle them in your
MediaSessionCompat.Callback
.
Summary
Inherited methods |
From class
android.content.BroadcastReceiver
final
void
|
abortBroadcast()
|
final
void
|
clearAbortBroadcast()
|
final
boolean
|
getAbortBroadcast()
|
final
boolean
|
getDebugUnregister()
|
final
int
|
getResultCode()
|
final
String
|
getResultData()
|
final
Bundle
|
getResultExtras(boolean arg0)
|
final
BroadcastReceiver.PendingResult
|
goAsync()
|
final
boolean
|
isInitialStickyBroadcast()
|
final
boolean
|
isOrderedBroadcast()
|
abstract
void
|
onReceive(Context arg0, Intent arg1)
|
IBinder
|
peekService(Context arg0, Intent arg1)
|
final
void
|
setDebugUnregister(boolean arg0)
|
final
void
|
setOrderedHint(boolean arg0)
|
final
void
|
setResult(int arg0, String arg1, Bundle arg2)
|
final
void
|
setResultCode(int arg0)
|
final
void
|
setResultData(String arg0)
|
final
void
|
setResultExtras(Bundle arg0)
|
|
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
public MediaButtonReceiver ()
Public methods
buildMediaButtonPendingIntent
public static 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
, and sent to the provided media
button receiver via the pending intent. The action
should be one of the following:
Parameters |
context |
Context : The context of the application. |
mbrComponent |
ComponentName : The full component name of a media button receiver where you want to send
this intent. |
action |
long : The action to be sent via the pending intent. |
Returns |
PendingIntent |
Created pending intent, or null if the given component name is null or the
action is unsupported/invalid.
|
buildMediaButtonPendingIntent
public static 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
, and it will be sent to the
registered media button receiver in the given context. The action
should be one of
the following:
Parameters |
context |
Context : The context of the application. |
action |
long : The action to be sent via the pending intent. |
Returns |
PendingIntent |
Created pending intent, or null if cannot find a unique registered media button
receiver or if the action is unsupported/invalid.
|
onReceive
public void onReceive (Context context,
Intent intent)
Parameters |
context |
Context |
intent |
Intent |