public class MediaButtonReceiver extends BroadcastReceiver
<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
in your app that controls
media playback via a MediaSessionCompat
- all Intent
s received by
the MediaButtonReceiver will be forwarded to that service.
First priority is given to a Service
that includes 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>If such a
Service
is not found, MediaButtonReceiver will attempt to
find a media browser service implementation.
If neither is available or more than one valid service/media browser service is found, an
IllegalStateException
will be thrown.
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
.BroadcastReceiver.PendingResult
Constructor and Description |
---|
MediaButtonReceiver() |
Modifier and Type | Method and Description |
---|---|
static PendingIntent |
buildMediaButtonPendingIntent(Context context,
ComponentName mbrComponent,
long action)
Creates a broadcast pending intent that will send a media button event.
|
static PendingIntent |
buildMediaButtonPendingIntent(Context context,
long action)
Creates a broadcast pending intent that will send a media button event.
|
static KeyEvent |
handleIntent(MediaSessionCompat mediaSessionCompat,
Intent intent)
Extracts any available
KeyEvent from an Intent.ACTION_MEDIA_BUTTON
intent, passing it onto the MediaSessionCompat using
MediaControllerCompat.dispatchMediaButtonEvent(KeyEvent) , which in turn
will trigger callbacks to the MediaSessionCompat.Callback registered via
MediaSessionCompat.setCallback(MediaSessionCompat.Callback) . |
void |
onReceive(Context context,
Intent intent)
This method is called when the BroadcastReceiver is receiving an Intent
broadcast.
|
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getPendingResult, getResultCode, getResultData, getResultExtras, getSendingUserId, goAsync, isInitialStickyBroadcast, isOrderedBroadcast, peekService, setDebugUnregister, setOrderedHint, setPendingResult, setResult, setResultCode, setResultData, setResultExtras
public void onReceive(Context context, Intent intent)
BroadcastReceiver
Context.registerReceiver(BroadcastReceiver,
IntentFilter, String, android.os.Handler)
. When it runs on the main
thread you should
never perform long-running operations in it (there is a timeout of
10 seconds that the system allows before considering the receiver to
be blocked and a candidate to be killed). You cannot launch a popup dialog
in your implementation of onReceive().
If this BroadcastReceiver was launched through a <receiver> tag,
then the object is no longer alive after returning from this
function. This means you should not perform any operations that
return a result to you asynchronously -- in particular, for interacting
with services, you should use
Context.startService(Intent)
instead of
Context.bindService(Intent, ServiceConnection, int)
. If you wish
to interact with a service that is already running, you can use
BroadcastReceiver.peekService(android.content.Context, android.content.Intent)
.
The Intent filters used in Context.registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)
and in application manifests are not guaranteed to be exclusive. They
are hints to the operating system about how to find suitable recipients. It is
possible for senders to force delivery to specific recipients, bypassing filter
resolution. For this reason, onReceive()
implementations should respond only to known actions, ignoring any unexpected
Intents that they may receive.
onReceive
in class BroadcastReceiver
context
- The Context in which the receiver is running.intent
- The Intent being received.public static KeyEvent handleIntent(MediaSessionCompat mediaSessionCompat, Intent intent)
KeyEvent
from an Intent.ACTION_MEDIA_BUTTON
intent, passing it onto the MediaSessionCompat
using
MediaControllerCompat.dispatchMediaButtonEvent(KeyEvent)
, which in turn
will trigger callbacks to the MediaSessionCompat.Callback
registered via
MediaSessionCompat.setCallback(MediaSessionCompat.Callback)
.
The returned KeyEvent
is non-null if any KeyEvent
is found and can
be used if any additional processing is needed beyond what is done in the
MediaSessionCompat.Callback
. An example of is to prevent redelivery of a
KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
Intent in the case of the Service being
restarted (which, by default, will redeliver the last received Intent).
KeyEvent keyEvent = MediaButtonReceiver.handleIntent(mediaSession, intent); if (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) { Intent emptyIntent = new Intent(intent); emptyIntent.setAction(""); startService(emptyIntent); }
mediaSessionCompat
- A MediaSessionCompat
that has a
MediaSessionCompat.Callback
set.intent
- The intent to parse.KeyEvent
if found, or null.public static PendingIntent buildMediaButtonPendingIntent(Context context, long action)
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:
context
- The context of the application.action
- The action to be sent via the pending intent.action
is unsupported/invalid.public static PendingIntent buildMediaButtonPendingIntent(Context context, ComponentName mbrComponent, long action)
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:
context
- The context of the application.mbrComponent
- The full component name of a media button receiver where you want to send
this intent.action
- The action to be sent via the pending intent.action
is unsupported/invalid.