public class MediaRouteActionProvider
extends ActionProvider

   ↳ androidx.core.view.ActionProvider

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. You can call setAlwaysVisible(boolean) to override this behavior.


To use the media route action provider, the activity must be a subclass of AppCompatActivity from the support library. Refer to support library documentation for details.


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=""
     <item android:id="@+id/media_route_menu_item"

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) {

         mRouter = Mediarouter.getInstance(this);
         mSelector = new MediaRouteSelector.Builder()
         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() {

         mediaRouter.addCallback(mSelector, mCallback,

         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);


     // Remove the callback when the activity is destroyed.
     public void onDestroy() {


     public boolean onCreateOptionsMenu(Menu menu) {

         getMenuInflater().inflate(, menu);

         MenuItem mediaRouteMenuItem = menu.findItem(;
         MediaRouteActionProvider mediaRouteActionProvider =
         return true;

     private final class MyCallback extends MediaRouter.Callback {
         // Implement callback methods as needed.


Public constructors

MediaRouteActionProvider(Context context)

Creates the action provider.

Public methods

void enableDynamicGroup()

This method is deprecated. Use