public class Presentation extends Dialog
A presentation is a special kind of dialog whose purpose is to present
content on a secondary display. A Presentation
is associated with
the target Display
at creation time and configures its context and
resource configuration according to the display's metrics.
Notably, the Context
of a presentation is different from the context
of its containing Activity
. It is important to inflate the layout
of a presentation and load other resources using the presentation's own context
to ensure that assets of the correct size and density for the target display
are loaded.
A presentation is automatically canceled (see Dialog.cancel()
) when
the display to which it is attached is removed. An activity should take
care of pausing and resuming whatever content is playing within the presentation
whenever the activity itself is paused or resumed.
Before showing a Presentation
it's important to choose the Display
on which it will appear. Choosing a presentation display is sometimes difficult
because there may be multiple displays attached. Rather than trying to guess
which display is best, an application should let the system choose a suitable
presentation display.
There are two main ways to choose a Display
.
The easiest way to choose a presentation display is to use the
MediaRouter
API. The media router service keeps
track of which audio and video routes are available on the system.
The media router sends notifications whenever routes are selected or unselected
or when the preferred presentation display of a route changes.
So an application can simply watch for these notifications and show or dismiss
a presentation on the preferred presentation display automatically.
The preferred presentation display is the display that the media router recommends that the application should use if it wants to show content on the secondary display. Sometimes there may not be a preferred presentation display in which case the application should show its content locally without using a presentation.
Here's how to use the media router to create and show a presentation on the preferred
presentation display using MediaRouter.RouteInfo.getPresentationDisplay()
.
MediaRouter mediaRouter = (MediaRouter) context.getSystemService(Context.MEDIA_ROUTER_SERVICE); MediaRouter.RouteInfo route = mediaRouter.getSelectedRoute(); if (route != null) { Display presentationDisplay = route.getPresentationDisplay(); if (presentationDisplay != null) { Presentation presentation = new MyPresentation(context, presentationDisplay); presentation.show(); } }
The following sample code from ApiDemos
demonstrates how to use the media
router to automatically switch between showing content in the main activity and showing
the content in a presentation when a presentation display is available.
Another way to choose a presentation display is to use the DisplayManager
API
directly. The display manager service provides functions to enumerate and describe all
displays that are attached to the system including displays that may be used
for presentations.
The display manager keeps track of all displays in the system. However, not all displays are appropriate for showing presentations. For example, if an activity attempted to show a presentation on the main display it might obscure its own content (it's like opening a dialog on top of your activity).
Here's how to identify suitable displays for showing presentations using
DisplayManager.getDisplays(String)
and the
DisplayManager.DISPLAY_CATEGORY_PRESENTATION
category.
DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE); Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); if (presentationDisplays.length > 0) { // If there is more than one suitable presentation display, then we could consider // giving the user a choice. For this example, we simply choose the first display // which is the one the system recommends as the preferred presentation display. Display display = presentationDisplays[0]; Presentation presentation = new MyPresentation(context, presentationDisplay); presentation.show(); }
The following sample code from ApiDemos
demonstrates how to use the display
manager to enumerate displays and show content on multiple presentation displays
simultaneously.
DialogInterface.OnCancelListener, DialogInterface.OnClickListener, DialogInterface.OnDismissListener, DialogInterface.OnKeyListener, DialogInterface.OnMultiChoiceClickListener, DialogInterface.OnShowListener
mCancelable
BUTTON_NEGATIVE, BUTTON_NEUTRAL, BUTTON_POSITIVE, BUTTON1, BUTTON2, BUTTON3
Constructor and Description |
---|
Presentation(Context outerContext,
Display display)
Creates a new presentation that is attached to the specified display
using the default theme.
|
Presentation(Context outerContext,
Display display,
int theme)
Creates a new presentation that is attached to the specified display
using the optionally specified theme.
|
Modifier and Type | Method and Description |
---|---|
Display |
getDisplay()
Gets the
Display that this presentation appears on. |
Resources |
getResources()
Gets the
Resources that should be used to inflate the layout of this presentation. |
void |
onDisplayChanged()
Called by the system when the properties of the
Display to which
the presentation is attached have changed. |
void |
onDisplayRemoved()
Called by the system when the
Display to which the presentation
is attached has been removed. |
protected void |
onStart()
Called when the dialog is starting.
|
protected void |
onStop()
Called to tell you that you're stopping.
|
void |
show()
Inherited from
Dialog.show() . |
addContentView, cancel, closeOptionsMenu, create, dismiss, dispatchGenericMotionEvent, dispatchKeyEvent, dispatchKeyShortcutEvent, dispatchPopulateAccessibilityEvent, dispatchTouchEvent, dispatchTrackballEvent, findViewById, getActionBar, getContext, getCurrentFocus, getLayoutInflater, getOwnerActivity, getSearchEvent, getVolumeControlStream, getWindow, hide, invalidateOptionsMenu, isShowing, onActionModeFinished, onActionModeStarted, onAttachedToWindow, onBackPressed, onContentChanged, onContextItemSelected, onContextMenuClosed, onCreate, onCreateContextMenu, onCreateOptionsMenu, onCreatePanelMenu, onCreatePanelView, onDetachedFromWindow, onGenericMotionEvent, onKeyDown, onKeyLongPress, onKeyMultiple, onKeyShortcut, onKeyUp, onMenuItemSelected, onMenuOpened, onOptionsItemSelected, onOptionsMenuClosed, onPanelClosed, onPrepareOptionsMenu, onPreparePanel, onRestoreInstanceState, onSaveInstanceState, onSearchRequested, onSearchRequested, onTouchEvent, onTrackballEvent, onWindowAttributesChanged, onWindowDismissed, onWindowFocusChanged, onWindowStartingActionMode, onWindowStartingActionMode, openContextMenu, openOptionsMenu, registerForContextMenu, requestWindowFeature, setCancelable, setCanceledOnTouchOutside, setCancelMessage, setContentView, setContentView, setContentView, setDismissMessage, setFeatureDrawable, setFeatureDrawableAlpha, setFeatureDrawableResource, setFeatureDrawableUri, setOnCancelListener, setOnDismissListener, setOnKeyListener, setOnShowListener, setOwnerActivity, setTitle, setTitle, setVolumeControlStream, takeCancelAndDismissListeners, takeKeyEvents, unregisterForContextMenu
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
onProvideKeyboardShortcuts
public Presentation(Context outerContext, Display display)
outerContext
- The context of the application that is showing the presentation.
The presentation will create its own context (see Dialog.getContext()
) based
on this context and information about the associated display.display
- The display to which the presentation should be attached.public Presentation(Context outerContext, Display display, int theme)
outerContext
- The context of the application that is showing the presentation.
The presentation will create its own context (see Dialog.getContext()
) based
on this context and information about the associated display.display
- The display to which the presentation should be attached.theme
- A style resource describing the theme to use for the window.
See
Style and Theme Resources for more information about defining and using
styles. This theme is applied on top of the current theme in
outerContext. If 0, the default presentation theme will be used.public Display getDisplay()
Display
that this presentation appears on.public Resources getResources()
Resources
that should be used to inflate the layout of this presentation.
This resources object has been configured according to the metrics of the
display that the presentation appears on.protected void onStart()
Dialog
protected void onStop()
Dialog
public void show()
Dialog.show()
. Will throw
WindowManager.InvalidDisplayException
if the specified secondary
Display
can't be found.public void onDisplayRemoved()
Display
to which the presentation
is attached has been removed.
The system automatically calls Dialog.cancel()
to dismiss the presentation
after sending this event.getDisplay()
public void onDisplayChanged()
Display
to which
the presentation is attached have changed.
If the display metrics have changed (for example, if the display has been
resized or rotated), then the system automatically calls
Dialog.cancel()
to dismiss the presentation.getDisplay()