MediaRouteActionProvider
public class MediaRouteActionProvider extends ActionProvider
| java.lang.Object | ||
| ↳ | androidx.core.view.ActionProvider | |
| ↳ | androidx.mediarouter.app.MediaRouteActionProvider |
The media route action provider displays a media route button in the application's ActionBar to allow the user to select routes and to control the currently selected route.
The application must specify the kinds of routes that the user should be allowed to select by specifying a selector with the setRouteSelector method.
Refer to MediaRouteButton for a description of the button that will appear in the action bar menu.
Prerequisites
To use the media route action provider, the activity must be a subclass of AppCompatActivity from the android.support.v7.appcompat support library. Refer to support library documentation for details.
Example
The application should define a menu resource to include the provider in the action bar options menu. Note that the support library action bar uses attributes that are defined in the application's resource namespace rather than the framework's resource namespace to configure each item.
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/media_route_menu_item" android:title="@string/media_route_menu_title" app:showAsAction="always" app:actionProviderClass="androidx.mediarouter.app.MediaRouteActionProvider"/> </menu>
Then configure the menu and set the route selector for the chooser.
public class MyActivity extends AppCompatActivity { private MediaRouter mRouter; private MediaRouter.Callback mCallback; private MediaRouteSelector mSelector; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mRouter = MediaRouter.getInstance(this); mSelector = new MediaRouteSelector.Builder() .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO) .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) .build(); mCallback = new MyCallback(); } // Add the callback on start to tell the media router what kinds of routes // the application is interested in so that it can try to discover suitable ones. public void onStart() { super.onStart(); mediaRouter.addCallback(mSelector, mCallback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); MediaRouter.RouteInfo route = mediaRouter.updateSelectedRoute(mSelector); // do something with the route... } // Remove the callback flag CALLBACK_FLAG_REQUEST_DISCOVERY on stop by calling // addCallback() again in order to tell the media router that it no longer // needs to invest effort trying to discover routes of these kinds for now. public void onStop() { mRouter.addCallback(mSelector, mCallback, /* flags= */ 0); super.onStop(); } // Remove the callback when the activity is destroyed. public void onDestroy() { mRouter.removeCallback(mCallback); super.onDestroy(); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); getMenuInflater().inflate(R.menu.sample_media_router_menu, menu); MenuItem mediaRouteMenuItem = menu.findItem(R.id.media_route_menu_item); MediaRouteActionProvider mediaRouteActionProvider = (MediaRouteActionProvider)MenuItemCompat.getActionProvider(mediaRouteMenuItem); mediaRouteActionProvider.setRouteSelector(mSelector); return true; } private final class MyCallback extends MediaRouter.Callback { // Implement callback methods as needed. } }
| See also | |
|---|---|
setRouteSelector |
Summary
Public constructors |
|---|
MediaRouteActionProvider(@NonNull Context context)Creates the action provider. |
Public methods |
|
|---|---|
void |
This method is deprecated. Use |
@NonNull MediaRouteDialogFactory |
Gets the media route dialog factory to use when showing the route chooser or controller dialog. |
@Nullable MediaRouteButton |
Gets the associated media route button, or null if it has not yet been created. |
@NonNull MediaRouteSelector |
Gets the media route selector for filtering the routes that the user can select using the media route chooser dialog. |
@NonNull View |
Factory method for creating new action views. |
@NonNull MediaRouteButton |
Called when the media route button is being created. |
boolean |
Performs an optional default action. |
void |
This method is deprecated. The visibility of the button is no longer controlled from |
void |
setDialogFactory(@NonNull MediaRouteDialogFactory factory)Sets the media route dialog factory to use when showing the route chooser or controller dialog. |
void |
setRouteSelector(@NonNull MediaRouteSelector selector)Sets the media route selector for filtering the routes that the user can select using the media route chooser dialog. |
Inherited methods |
||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
public void enableDynamicGroup()Enables dynamic group feature. With this enabled, a different set of MediaRouteChooserDialog and MediaRouteControllerDialog is shown when the button is clicked. If a media route provider supports dynamic group, the users can use that feature with the dialogs.
getDialogFactory
public @NonNull MediaRouteDialogFactory getDialogFactory()
Gets the media route dialog factory to use when showing the route chooser or controller dialog.
| Returns | |
|---|---|
@NonNull MediaRouteDialogFactory |
The dialog factory, never null. |
getMediaRouteButton
public @Nullable MediaRouteButton getMediaRouteButton()
Gets the associated media route button, or null if it has not yet been created.
getRouteSelector
public @NonNull MediaRouteSelector getRouteSelector()
Gets the media route selector for filtering the routes that the user can select using the media route chooser dialog.
| Returns | |
|---|---|
@NonNull MediaRouteSelector |
The selector, never null. |
onCreateActionView
public @NonNull View onCreateActionView()
Factory method for creating new action views.
onCreateMediaRouteButton
public @NonNull MediaRouteButton onCreateMediaRouteButton()
Called when the media route button is being created.
Subclasses may override this method to customize the button.
onPerformDefaultAction
public boolean onPerformDefaultAction()
Performs an optional default action.
For the case of an action provider placed in a menu item not shown as an action this method is invoked if previous callbacks for processing menu selection has handled the event.
A menu item selection is processed in the following order:
- Receiving a call to
MenuItem.OnMenuItemClickListener.onMenuItemClick. - Receiving a call to
onOptionsItemSelectedFragmentActivity.onOptionsItemSelected(MenuItem)} - Receiving a call to
onOptionsItemSelectedFragment.onOptionsItemSelected(MenuItem)} - Launching the
android.content.Intentset viaMenuItem.setIntent(android.content.Intent) - Invoking this method.
The default implementation does not perform any action and returns false.
setDialogFactory
public void setDialogFactory(@NonNull MediaRouteDialogFactory factory)
Sets the media route dialog factory to use when showing the route chooser or controller dialog.
| Parameters | |
|---|---|
@NonNull MediaRouteDialogFactory factory |
The dialog factory, must not be null. |
setRouteSelector
public void setRouteSelector(@NonNull MediaRouteSelector selector)
Sets the media route selector for filtering the routes that the user can select using the media route chooser dialog.
| Parameters | |
|---|---|
@NonNull MediaRouteSelector selector |
The selector, must not be null. |