Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)
Android APIs
public class

MediaRouteActionProvider

extends ActionProvider
java.lang.Object
   ↳ android.support.v4.view.ActionProvider
     ↳ android.support.v7.app.MediaRouteActionProvider

Class Overview

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(MediaRouteSelector) method.

Refer to MediaRouteButton for a description of the button that will appear in the action bar menu. Note that instead of disabling the button when no routes are available, the action provider will instead make the menu item invisible. In this way, the button will only be visible when it is possible for the user to discover and select a matching route.

Prerequisites

To use the media route action provider, the activity must be a subclass of ActionBarActivity 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="android.support.v7.app.MediaRouteActionProvider"/>
 </menu>
      

Then configure the menu and set the route selector for the chooser.

       public class MyActivity extends ActionBarActivity {
     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 selector on stop 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() {
         super.onStop();

         mediaRouter.removeCallback(mCallback);
     }

     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.
     }
 }
      

Summary

Public Constructors
MediaRouteActionProvider ( Context context)
Creates the action provider.
Public Methods
MediaRouteDialogFactory getDialogFactory ()
Gets the media route dialog factory to use when showing the route chooser or controller dialog.
MediaRouteButton getMediaRouteButton ()
Gets the associated media route button, or null if it has not yet been created.
MediaRouteSelector getRouteSelector ()
Gets the media route selector for filtering the routes that the user can select using the media route chooser dialog.
boolean isVisible ()
If overridesItemVisibility() returns true, the return value of this method will help determine the visibility of the MenuItem this ActionProvider is bound to.
View onCreateActionView ()
Factory method for creating new action views.
MediaRouteButton onCreateMediaRouteButton ()
Called when the media route button is being created.
boolean onPerformDefaultAction ()
Performs an optional default action.
boolean overridesItemVisibility ()
The result of this method determines whether or not isVisible() will be used by the MenuItem this ActionProvider is bound to help determine its visibility.
void setDialogFactory ( MediaRouteDialogFactory factory)
Sets the media route dialog factory to use when showing the route chooser or controller dialog.
void setRouteSelector ( MediaRouteSelector selector)
Sets the media route selector for filtering the routes that the user can select using the media route chooser dialog.
[Expand]
Inherited Methods
From class android.support.v4.view.ActionProvider
From class java.lang.Object

Public Constructors

public MediaRouteActionProvider ( Context context)

Creates the action provider.

Parameters
context The context.

Public Methods

public MediaRouteDialogFactory getDialogFactory ()

Gets the media route dialog factory to use when showing the route chooser or controller dialog.

Returns
  • The dialog factory, never null.

public MediaRouteButton getMediaRouteButton ()

Gets the associated media route button, or null if it has not yet been created.

public MediaRouteSelector getRouteSelector ()

Gets the media route selector for filtering the routes that the user can select using the media route chooser dialog.

Returns
  • The selector, never null.

public boolean isVisible ()

If overridesItemVisibility() returns true, the return value of this method will help determine the visibility of the MenuItem this ActionProvider is bound to.

If the MenuItem's visibility is explicitly set to false by the application, the MenuItem will not be shown, even if this method returns true.

Returns
  • true if the MenuItem this ActionProvider is bound to is visible, false if it is invisible. The default implementation returns true.

public View onCreateActionView ()

Factory method for creating new action views.

Returns
  • A new action view.

public MediaRouteButton onCreateMediaRouteButton ()

Called when the media route button is being created.

Subclasses may override this method to customize the button.

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:

The default implementation does not perform any action and returns false.

public boolean overridesItemVisibility ()

The result of this method determines whether or not isVisible() will be used by the MenuItem this ActionProvider is bound to help determine its visibility.

Returns
  • true if this ActionProvider overrides the visibility of the MenuItem it is bound to, false otherwise. The default implementation returns false.

public void setDialogFactory ( MediaRouteDialogFactory factory)

Sets the media route dialog factory to use when showing the route chooser or controller dialog.

Parameters
factory The dialog factory, must not be null.

public void setRouteSelector ( MediaRouteSelector selector)

Sets the media route selector for filtering the routes that the user can select using the media route chooser dialog.

Parameters
selector The selector, must not be null.