MediaRouteButtonFactory
@MainThread
@UnstableApi
class MediaRouteButtonFactory
A factory class to set up a media route button.
Must be called on the main thread.
Summary
Public functions |
|
|---|---|
java-static ListenableFuture<Void!>! |
@CanIgnoreReturnValueSets up a media route button with an asynchronous callback, which will not block the caller thread. |
java-static ListenableFuture<MenuItem!>! |
@CanIgnoreReturnValueSets up a media route button in the action bar menu with an asynchronous callback, which will not block the caller thread. |
Public functions
setUpMediaRouteButton
@CanIgnoreReturnValue
java-static fun setUpMediaRouteButton(context: Context!, button: MediaRouteButton!): ListenableFuture<Void!>!
Sets up a media route button with an asynchronous callback, which will not block the caller thread.
The application can add a MediaRouteButton to their activity layout .xml file. Then the application can set up the media route button as follows.
public class MyActivity extends AppCompatActivity { ... @Override public void onCreate(Bundle savedInstanceState) { ... MediaRouteButton button = findViewById(R.id.media_route_button); ListenableFuture<Void> setUpFuture = MediaRouteButtonFactory.setUpMediaRouteButton(this, button); Futures.addCallback( setUpFuture, new FutureCallback<Void>() { @Override public void onSuccess(Void unused) { // Indicate that the media route button is set up successfully. } @Override public void onFailure(Throwable t) { // Handle the failure. } }, executor); ... } }
If setting up the media route button succeeds, the future will resolve to null and indicate that the media route button is set up successfully.
If setting up the media route button fails, the future may fail with an exception. Consumers should handle the failure gracefully, for example by not showing the media route button.
The method must be called on the main thread. The returned ListenableFuture may be called on a different thread than the caller's thread. If the caller wants to update the UI in the callbacks, it is responsible for forwarding the callback to the UI thread.
Clicking on the media route button opens a dialog that allows the user to select a remote device for transferring media.
See MediaRouteButton for more details.
| Parameters | |
|---|---|
context: Context! |
The |
button: MediaRouteButton! |
The |
| Returns | |
|---|---|
ListenableFuture<Void!>! |
A |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
If the media route button is null. |
java.lang.IllegalStateException |
If any of the following conditions occur:
|
setUpMediaRouteButton
@CanIgnoreReturnValue
java-static fun setUpMediaRouteButton(context: Context!, menu: Menu!, menuResourceId: Int): ListenableFuture<MenuItem!>!
Sets up a media route button in the action bar menu with an asynchronous callback, which will not block the caller thread. Returns a ListenableFuture with the menu item of the media route button
The application should define a menu resource to include the androidx.mediarouter.app.MediaRouteActionProvider as the action provider of the menu 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>
public class MyActivity extends AppCompatActivity { ... @Override public boolean onCreateOptionsMenu(Menu menu) { ... getMenuInflater().inflate(R.menu.sample_media_route_button_menu, menu); ListenableFuture<MenuItem> menuItemFuture = MediaRouteButtonFactory.setUpMediaRouteButton(this, menu, R.id.media_route_menu_item); Futures.addCallback( menuItemFuture, new FutureCallback<MenuItem>() { @Override public void onSuccess(MenuItem menuItem) { // Do something with the menu item. } @Override public void onFailure(Throwable t) { // Handle the failure. } }, executor); ... } }
If setting up the media route button succeeds, the future will resolve to the menu item of the media route button. The application can do further operations with the menu item, such as showing an introductory overlay to highlight the media route button to users.
If setting up the media route button fails, the future may fail with an exception. Consumers should handle the failure gracefully, for example by not showing the media route button.
The method must be called on the main thread. The callback of the returned ListenableFuture may be called on a different thread than the caller's thread. If the caller wants to update the UI in the callbacks, it is responsible for forwarding the callback to the UI thread.
Clicking on the media route button opens a dialog that allows the user to select a remote device for transferring media.
See androidx.mediarouter.app.MediaRouteActionProvider for more details.
| Parameters | |
|---|---|
context: Context! |
The |
menu: Menu! |
The |
menuResourceId: Int |
The resource ID of the menu item for the media route button. |
| Returns | |
|---|---|
ListenableFuture<MenuItem!>! |
A |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if the menu doesn't contain a menu item with the given |
java.lang.IllegalStateException |
If any of the following condition occurs:
|