android 双屏显示机制
Presentation
extends Dialogjava.lang.Object | ||
↳ | android.app.Dialog | |
↳ | android.app.Presentation |
Class Overview
Base class for presentations.
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 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.
Choosing a presentation display
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
.
Using the media router to choose a presentation 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 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.
/**
* <h3>Presentation Activity</h3>
*
* <p>
* This demonstrates how to create an activity that shows some content
* on a secondary display using a {@link Presentation}.
* </p><p>
* The activity uses the {@link MediaRouter} API to automatically detect when
* a presentation display is available and to allow the user to control the
* media routes using a menu item. When a presentation display is available,
* we stop showing content in the main activity and instead open up a
* {@link Presentation} on the preferred presentation display. When a presentation
* display is removed, we revert to showing content in the main activity.
* We also write information about displays and display-related events to
* the Android log which you can read using <code>adb logcat</code>.
* </p><p>
* You can try this out using an HDMI or Wifi display or by using the
* "Simulate secondary displays" feature in Development Settings to create a few
* simulated secondary displays. Each display will appear in the list along with a
* checkbox to show a presentation on that display.
* </p><p>
* See also the {@link PresentationActivity} sample which
* uses the low-level display manager to enumerate displays and to show multiple
* simultaneous presentations on different displays.
* </p>
*/
public class PresentationWithMediaRouterActivity extends Activity {
private final String TAG = "PresentationWithMediaRouterActivity";
private MediaRouter mMediaRouter;
private DemoPresentation mPresentation;
private GLSurfaceView mSurfaceView;
private TextView mInfoTextView;
private boolean mPaused;
/**
* Initialization of the Activity after it is first created. Must at least
* call {@link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
// Get the media router service.
mMediaRouter = (MediaRouter)getSystemService(Context.MEDIA_ROUTER_SERVICE);
// See assets/res/any/layout/presentation_with_media_router_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.presentation_with_media_router_activity);
// Set up the surface view for visual interest.
mSurfaceView = (GLSurfaceView)findViewById(R.id.surface_view);
mSurfaceView.setRenderer(new CubeRenderer(false));
// Get a text view where we will show information about what's happening.
mInfoTextView = (TextView)findViewById(R.id.info);
}
@Override
protected void onResume() {
// Be sure to call the super class.
super.onResume();
// Listen for changes to media routes.
mMediaRouter.addCallback(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouterCallback);
// Update the presentation based on the currently selected route.
mPaused = false;
updatePresentation();
}
@Override
protected void onPause() {
// Be sure to call the super class.
super.onPause();
// Stop listening for changes to media routes.
mMediaRouter.removeCallback(mMediaRouterCallback);
// Pause rendering.
mPaused = true;
updateContents();
}
@Override
protected void onStop() {
// Be sure to call the super class.
super.onStop();