Android | 说说Presentation

2024-06-06 19:08
文章标签 android presentation

本文主要是介绍Android | 说说Presentation,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1、什么是Presentation

2、获取屏幕

2.1 使用media router来获取可以显示presentation的屏幕

2.2 通过Display Manager来获取presentation display

3、Presentation说明

3.1 继承的常量

3.2 构造函数

3.3 公共方法

3.4 Protected 方法

4、其他


1、什么是Presentation

Presentation是什么呢?先看看官方文档里对Presentation的描述:

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.(翻译:Presentation是一种特殊的对话框,用于在辅助屏幕上显示内容。Presentation在创建时与目标屏幕相关联并根据目标屏幕的属性进行context和资源配置)

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.(翻译:要注意的是,Presentation的Context和包含它的Activity的context是不同的。填充Presentation的布局并通过Presentation本身的context来加载资源,这样才能保证目标屏幕的正确大小和密度被加载)

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.(翻译:当presentation 关联的屏幕被移除后,presentation会自动被cancel,当Activity本身pause和resume的时候,Activity应该处理presentation中的播放的任何内容)


2、获取屏幕

有两种方式可以选择Presentation要显示的屏幕。

2.1 使用media router来获取可以显示presentation的屏幕

使用MediaRouter API。

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.  (MediaRouter会提供一个最合适的屏幕供presentation显示)

Sometimes there may not be a preferred presentation display in which case the application should show its content locally without using a presentation.(有时候可能没有获取到最合适的屏幕,这种情况,就要在本地显示内容而不是使用presentation)

那么如何通过getPresentationDisplay()接口来使用media router来创建和显示presentation呢?如下是使用示例:

ediaRouter 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();}}

下面的示例代码展示了如何通过media router自动切换在activity中显示内容还是通过presentation显示内容。

/*** <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.*/@Overrideprotected 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);}@Overrideprotected 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();}@Overrideprotected 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();}@Overrideprotected void onStop() {// Be sure to call the super class.super.onStop();// Dismiss the presentation when the activity is not visible.if (mPresentation != null) {Log.i(TAG, "Dismissing presentation because the activity is no longer visible.");mPresentation.dismiss();mPresentation = null;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Be sure to call the super class.super.onCreateOptionsMenu(menu);// Inflate the menu and configure the media router action provider.getMenuInflater().inflate(R.menu.presentation_with_media_router_menu, menu);MenuItem mediaRouteMenuItem = menu.findItem(R.id.menu_media_route);MediaRouteActionProvider mediaRouteActionProvider =(MediaRouteActionProvider)mediaRouteMenuItem.getActionProvider();mediaRouteActionProvider.setRouteTypes(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);// Return true to show the menu.return true;}private void updatePresentation() {// Get the current route and its presentation display.MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);Display presentationDisplay = route != null ? route.getPresentationDisplay() : null;// Dismiss the current presentation if the display has changed.if (mPresentation != null && mPresentation.getDisplay() != presentationDisplay) {Log.i(TAG, "Dismissing presentation because the current route no longer "+ "has a presentation display.");mPresentation.dismiss();mPresentation = null;}// Show a new presentation if needed.if (mPresentation == null && presentationDisplay != null) {Log.i(TAG, "Showing presentation on display: " + presentationDisplay);mPresentation = new DemoPresentation(this, presentationDisplay);mPresentation.setOnDismissListener(mOnDismissListener);try {mPresentation.show();} catch (WindowManager.InvalidDisplayException ex) {Log.w(TAG, "Couldn't show presentation!  Display was removed in "+ "the meantime.", ex);mPresentation = null;}}// Update the contents playing in this activity.updateContents();}private void updateContents() {// Show either the content in the main activity or the content in the presentation// along with some descriptive text about what is happening.if (mPresentation != null) {mInfoTextView.setText(getResources().getString(R.string.presentation_with_media_router_now_playing_remotely,mPresentation.getDisplay().getName()));mSurfaceView.setVisibility(View.INVISIBLE);mSurfaceView.onPause();if (mPaused) {mPresentation.getSurfaceView().onPause();} else {mPresentation.getSurfaceView().onResume();}} else {mInfoTextView.setText(getResources().getString(R.string.presentation_with_media_router_now_playing_locally,getWindowManager().getDefaultDisplay().getName()));mSurfaceView.setVisibility(View.VISIBLE);if (mPaused) {mSurfaceView.onPause();} else {mSurfaceView.onResume();}}}private final MediaRouter.SimpleCallback mMediaRouterCallback =new MediaRouter.SimpleCallback() {@Overridepublic void onRouteSelected(MediaRouter router, int type, RouteInfo info) {Log.d(TAG, "onRouteSelected: type=" + type + ", info=" + info);updatePresentation();}@Overridepublic void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {Log.d(TAG, "onRouteUnselected: type=" + type + ", info=" + info);updatePresentation();}@Overridepublic void onRoutePresentationDisplayChanged(MediaRouter router, RouteInfo info) {Log.d(TAG, "onRoutePresentationDisplayChanged: info=" + info);updatePresentation();}};/*** Listens for when presentations are dismissed.*/private final DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener() {@Overridepublic void onDismiss(DialogInterface dialog) {if (dialog == mPresentation) {Log.i(TAG, "Presentation was dismissed.");mPresentation = null;updateContents();}}};/*** The presentation to show on the secondary display.* <p>* Note that this display may have different metrics from the display on which* the main activity is showing so we must be careful to use the presentation's* own {@link Context} whenever we load resources.* </p>*/private final static class DemoPresentation extends Presentation {private GLSurfaceView mSurfaceView;public DemoPresentation(Context context, Display display) {super(context, display);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// Be sure to call the super class.super.onCreate(savedInstanceState);// Get the resources for the context of the presentation.// Notice that we are getting the resources from the context of the presentation.Resources r = getContext().getResources();// Inflate the layout.setContentView(R.layout.presentation_with_media_router_content);// Set up the surface view for visual interest.mSurfaceView = (GLSurfaceView)findViewById(R.id.surface_view);mSurfaceView.setRenderer(new CubeRenderer(false));}public GLSurfaceView getSurfaceView() {return mSurfaceView;}}
}

2.2 通过Display Manager来获取presentation display

(看英文原文更好理解)

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 getDisplays(String) and the 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.

*** <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 DisplayManager} API to enumerate displays.* When the user selects a display, the activity opens a {@link Presentation}* on that display.  We show a different photograph in each presentation* on a unique background along with a label describing the display.* 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 PresentationWithMediaRouterActivity} sample which* uses the media router to automatically select a secondary display* on which to show content based on the currently selected route.* </p>*/
public class PresentationActivity extends Activityimplements OnCheckedChangeListener, OnClickListener, OnItemSelectedListener {private final String TAG = "PresentationActivity";// Key for storing saved instance state.private static final String PRESENTATION_KEY = "presentation";// The content that we want to show on the presentation.private static final int[] PHOTOS = new int[] {R.drawable.frantic,R.drawable.photo1, R.drawable.photo2, R.drawable.photo3,R.drawable.photo4, R.drawable.photo5, R.drawable.photo6,R.drawable.sample_4,};private DisplayManager mDisplayManager;private DisplayListAdapter mDisplayListAdapter;private CheckBox mShowAllDisplaysCheckbox;private ListView mListView;private int mNextImageNumber;// List of presentation contents indexed by displayId.// This state persists so that we can restore the old presentation// contents when the activity is paused or resumed.private SparseArray<DemoPresentationContents> mSavedPresentationContents;// List of all currently visible presentations indexed by display id.private final SparseArray<DemoPresentation> mActivePresentations =new SparseArray<DemoPresentation>();/*** 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.*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// Be sure to call the super class.super.onCreate(savedInstanceState);// Restore saved instance state.if (savedInstanceState != null) {mSavedPresentationContents =savedInstanceState.getSparseParcelableArray(PRESENTATION_KEY);} else {mSavedPresentationContents = new SparseArray<DemoPresentationContents>();}// Get the display manager service.mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);// See assets/res/any/layout/presentation_activity.xml for this// view layout definition, which is being set here as// the content of our screen.setContentView(R.layout.presentation_activity);// Set up checkbox to toggle between showing all displays or only presentation displays.mShowAllDisplaysCheckbox = (CheckBox)findViewById(R.id.show_all_displays);mShowAllDisplaysCheckbox.setOnCheckedChangeListener(this);// Set up the list of displays.mDisplayListAdapter = new DisplayListAdapter(this);mListView = (ListView)findViewById(R.id.display_list);mListView.setAdapter(mDisplayListAdapter);}@Overrideprotected void onResume() {// Be sure to call the super class.super.onResume();// Update our list of displays on resume.mDisplayListAdapter.updateContents();// Restore presentations from before the activity was paused.final int numDisplays = mDisplayListAdapter.getCount();for (int i = 0; i < numDisplays; i++) {final Display display = mDisplayListAdapter.getItem(i);final DemoPresentationContents contents =mSavedPresentationContents.get(display.getDisplayId());if (contents != null) {showPresentation(display, contents);}}mSavedPresentationContents.clear();// Register to receive events from the display manager.mDisplayManager.registerDisplayListener(mDisplayListener, null);}@Overrideprotected void onPause() {// Be sure to call the super class.super.onPause();// Unregister from the display manager.mDisplayManager.unregisterDisplayListener(mDisplayListener);// Dismiss all of our presentations but remember their contents.Log.d(TAG, "Activity is being paused.  Dismissing all active presentation.");for (int i = 0; i < mActivePresentations.size(); i++) {DemoPresentation presentation = mActivePresentations.valueAt(i);int displayId = mActivePresentations.keyAt(i);mSavedPresentationContents.put(displayId, presentation.mContents);presentation.dismiss();}mActivePresentations.clear();}@Overrideprotected void onSaveInstanceState(Bundle outState) {// Be sure to call the super class.super.onSaveInstanceState(outState);outState.putSparseParcelableArray(PRESENTATION_KEY, mSavedPresentationContents);}/*** Shows a {@link Presentation} on the specified display.*/private void showPresentation(Display display, DemoPresentationContents contents) {final int displayId = display.getDisplayId();if (mActivePresentations.get(displayId) != null) {return;}Log.d(TAG, "Showing presentation photo #" + contents.photo+ " on display #" + displayId + ".");DemoPresentation presentation = new DemoPresentation(this, display, contents);presentation.show();presentation.setOnDismissListener(mOnDismissListener);mActivePresentations.put(displayId, presentation);}/*** Hides a {@link Presentation} on the specified display.*/private void hidePresentation(Display display) {final int displayId = display.getDisplayId();DemoPresentation presentation = mActivePresentations.get(displayId);if (presentation == null) {return;}Log.d(TAG, "Dismissing presentation on display #" + displayId + ".");presentation.dismiss();mActivePresentations.delete(displayId);}/*** Sets the display mode of the {@link Presentation} on the specified display* if it is already shown.*/private void setPresentationDisplayMode(Display display, int displayModeId) {final int displayId = display.getDisplayId();DemoPresentation presentation = mActivePresentations.get(displayId);if (presentation == null) {return;}presentation.setPreferredDisplayMode(displayModeId);}private int getNextPhoto() {final int photo = mNextImageNumber;mNextImageNumber = (mNextImageNumber + 1) % PHOTOS.length;return photo;}/*** Called when the show all displays checkbox is toggled or when* an item in the list of displays is checked or unchecked.*/@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if (buttonView == mShowAllDisplaysCheckbox) {// Show all displays checkbox was toggled.mDisplayListAdapter.updateContents();} else {// Display item checkbox was toggled.final Display display = (Display)buttonView.getTag();if (isChecked) {DemoPresentationContents contents = new DemoPresentationContents(getNextPhoto());showPresentation(display, contents);} else {hidePresentation(display);}mDisplayListAdapter.updateContents();}}/*** Called when the Info button next to a display is clicked to show information* about the display.*/@Overridepublic void onClick(View v) {Context context = v.getContext();AlertDialog.Builder builder = new AlertDialog.Builder(context);final Display display = (Display)v.getTag();Resources r = context.getResources();AlertDialog alert = builder.setTitle(r.getString(R.string.presentation_alert_info_text, display.getDisplayId())).setMessage(display.toString()).setNeutralButton(R.string.presentation_alert_dismiss_text,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}}).create();alert.show();}/*** Called when a display mode has been selected.*/@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {final Display display = (Display)parent.getTag();final Display.Mode[] modes = display.getSupportedModes();setPresentationDisplayMode(display, position >= 1 && position <= modes.length ?modes[position - 1].getModeId() : 0);}/*** Called when a display mode has been unselected.*/@Overridepublic void onNothingSelected(AdapterView<?> parent) {final Display display = (Display)parent.getTag();setPresentationDisplayMode(display, 0);}/*** Listens for displays to be added, changed or removed.* We use it to update the list and show a new {@link Presentation} when a* display is connected.** Note that we don't bother dismissing the {@link Presentation} when a* display is removed, although we could.  The presentation API takes care* of doing that automatically for us.*/private final DisplayManager.DisplayListener mDisplayListener =new DisplayManager.DisplayListener() {@Overridepublic void onDisplayAdded(int displayId) {Log.d(TAG, "Display #" + displayId + " added.");mDisplayListAdapter.updateContents();}@Overridepublic void onDisplayChanged(int displayId) {Log.d(TAG, "Display #" + displayId + " changed.");mDisplayListAdapter.updateContents();}@Overridepublic void onDisplayRemoved(int displayId) {Log.d(TAG, "Display #" + displayId + " removed.");mDisplayListAdapter.updateContents();}};/*** Listens for when presentations are dismissed.*/private final DialogInterface.OnDismissListener mOnDismissListener =new DialogInterface.OnDismissListener() {@Overridepublic void onDismiss(DialogInterface dialog) {DemoPresentation presentation = (DemoPresentation)dialog;int displayId = presentation.getDisplay().getDisplayId();Log.d(TAG, "Presentation on display #" + displayId + " was dismissed.");mActivePresentations.delete(displayId);mDisplayListAdapter.notifyDataSetChanged();}};/*** List adapter.* Shows information about all displays.*/private final class DisplayListAdapter extends ArrayAdapter<Display> {final Context mContext;public DisplayListAdapter(Context context) {super(context, R.layout.presentation_list_item);mContext = context;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {final View v;if (convertView == null) {v = ((Activity) mContext).getLayoutInflater().inflate(R.layout.presentation_list_item, null);} else {v = convertView;}final Display display = getItem(position);final int displayId = display.getDisplayId();DemoPresentation presentation = mActivePresentations.get(displayId);DemoPresentationContents contents = presentation != null ?presentation.mContents : null;if (contents == null) {contents = mSavedPresentationContents.get(displayId);}CheckBox cb = (CheckBox)v.findViewById(R.id.checkbox_presentation);cb.setTag(display);cb.setOnCheckedChangeListener(PresentationActivity.this);cb.setChecked(contents != null);TextView tv = (TextView)v.findViewById(R.id.display_id);tv.setText(v.getContext().getResources().getString(R.string.presentation_display_id_text, displayId, display.getName()));Button b = (Button)v.findViewById(R.id.info);b.setTag(display);b.setOnClickListener(PresentationActivity.this);Spinner s = (Spinner)v.findViewById(R.id.modes);Display.Mode[] modes = display.getSupportedModes();if (contents == null || modes.length == 1) {s.setVisibility(View.GONE);s.setAdapter(null);} else {ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1);s.setVisibility(View.VISIBLE);s.setAdapter(modeAdapter);s.setTag(display);s.setOnItemSelectedListener(PresentationActivity.this);modeAdapter.add("<default mode>");for (Display.Mode mode : modes) {modeAdapter.add(String.format("Mode %d: %dx%d/%.1ffps",mode.getModeId(),mode.getPhysicalWidth(), mode.getPhysicalHeight(),mode.getRefreshRate()));if (contents.displayModeId == mode.getModeId()) {s.setSelection(modeAdapter.getCount() - 1);}}}return v;}/*** Update the contents of the display list adapter to show* information about all current displays.*/public void updateContents() {clear();String displayCategory = getDisplayCategory();Display[] displays = mDisplayManager.getDisplays(displayCategory);addAll(displays);Log.d(TAG, "There are currently " + displays.length + " displays connected.");for (Display display : displays) {Log.d(TAG, "  " + display);}}private String getDisplayCategory() {return mShowAllDisplaysCheckbox.isChecked() ? null :DisplayManager.DISPLAY_CATEGORY_PRESENTATION;}}/*** The presentation to show on the secondary display.** Note that the presentation display may have different metrics from the display on which* the main activity is showing so we must be careful to use the presentation's* own {@link Context} whenever we load resources.*/private final class DemoPresentation extends Presentation {final DemoPresentationContents mContents;public DemoPresentation(Context context, Display display,DemoPresentationContents contents) {super(context, display);mContents = contents;}/*** Sets the preferred display mode id for the presentation.*/public void setPreferredDisplayMode(int modeId) {mContents.displayModeId = modeId;WindowManager.LayoutParams params = getWindow().getAttributes();params.preferredDisplayModeId = modeId;getWindow().setAttributes(params);}@Overrideprotected void onCreate(Bundle savedInstanceState) {// Be sure to call the super class.super.onCreate(savedInstanceState);// Get the resources for the context of the presentation.// Notice that we are getting the resources from the context of the presentation.Resources r = getContext().getResources();// Inflate the layout.setContentView(R.layout.presentation_content);final Display display = getDisplay();final int displayId = display.getDisplayId();final int photo = mContents.photo;// Show a caption to describe what's going on.TextView text = (TextView)findViewById(R.id.text);text.setText(r.getString(R.string.presentation_photo_text,photo, displayId, display.getName()));// Show a n image for visual interest.ImageView image = (ImageView)findViewById(R.id.image);image.setImageDrawable(r.getDrawable(PHOTOS[photo]));GradientDrawable drawable = new GradientDrawable();drawable.setShape(GradientDrawable.RECTANGLE);drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);// Set the background to a random gradient.Point p = new Point();getDisplay().getSize(p);drawable.setGradientRadius(Math.max(p.x, p.y) / 2);drawable.setColors(mContents.colors);findViewById(android.R.id.content).setBackground(drawable);}}/*** Information about the content we want to show in the presentation.*/private final static class DemoPresentationContents implements Parcelable {final int photo;final int[] colors;int displayModeId;public static final Creator<DemoPresentationContents> CREATOR =new Creator<DemoPresentationContents>() {@Overridepublic DemoPresentationContents createFromParcel(Parcel in) {return new DemoPresentationContents(in);}@Overridepublic DemoPresentationContents[] newArray(int size) {return new DemoPresentationContents[size];}};public DemoPresentationContents(int photo) {this.photo = photo;colors = new int[] {((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000,((int) (Math.random() * Integer.MAX_VALUE)) | 0xFF000000 };}private DemoPresentationContents(Parcel in) {photo = in.readInt();colors = new int[] { in.readInt(), in.readInt() };displayModeId = in.readInt();}@Overridepublic int describeContents() {return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {dest.writeInt(photo);dest.writeInt(colors[0]);dest.writeInt(colors[1]);dest.writeInt(displayModeId);}}
}
  • for information on about live video routes and how to obtain the preferred presentation display for the current media route.
  • for information on how to enumerate displays and receive notifications when displays are added or removed.

3、Presentation说明

3.1 继承的常量

(来自android.content.DisplayInterface)

intBUTTON1This constant was deprecated in API level 3. Use BUTTON_POSITIVE
intBUTTON2This constant was deprecated in API level 3. Use BUTTON_NEGATIVE
intBUTTON3This constant was deprecated in API level 3. Use BUTTON_NEUTRAL
intBUTTON_NEGATIVEThe identifier for the negative button.
intBUTTON_NEUTRALThe identifier for the neutral button.
intBUTTON_POSITIVEThe identifier for the positive button.

3.2 构造函数

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.

函数说明:

1)public Presentation (Context outerContext, Display display)

参数:

outerContextThe context of the application that is showing the presentation. The presentation will create its own context (see getContext()) based on this context and information about the associated display.
displayThe display to which the presentation should be attached.

2)public Presentation (Context outerContext, Display display, int theme)

参数:

outerContextThe context of the application that is showing the presentation. The presentation will create its own context (see getContext()) based on this context and information about the associated display.
displayThe display to which the presentation should be attached.
themeA 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.

3.3 公共方法

DisplaygetDisplay()

Gets the Display that this presentation appears on.

ResourcesgetResources()

Gets the Resources that should be used to inflate the layout of this presentation.

voidonDisplayChanged()

Called by the system when the properties of the Display to which the presentation is attached have changed.

voidonDisplayRemoved()

Called by the system when the Display to which the presentation is attached has been removed.

voidshow()

Inherited from show().

函数说明:

函数定义返回值备注

public Display getDisplay ()

The display.

--

public Resources getResources ()

The presentation resources object.

--

public void onDisplayChanged ()

see getDisplay()Called by the system when the properties of the 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 cancel() to dismiss the presentation.

public void onDisplayRemoved ()

see getDisplay()Called by the system when the Display to which the presentation is attached has been removed. The system automatically calls cancel() to dismiss the presentation after sending this event.

public void show ()

--Inherited from show(). Will throw WindowManager.InvalidDisplayException if the specified secondary Display can't be found.

3.4 Protected 方法

voidonStart()

Called when the dialog is starting.

voidonStop()

Called to tell you that you're stopping.

4、其他

在Setting的开发者模式下有个“模拟辅助显示设备”,选择一个分辨率后界面就会出现一个virtual display,如果APP 使用了presentation来显示内容,那么可以控制presentation中显示的内容,使得virtual display和界面显示不同的内容,做到双屏异显。更多相关信息可以参考:

Android Presentation是什么

Android7.1 Presentation双屏异显原理分析

这篇关于Android | 说说Presentation的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1036970

相关文章

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存

从状态管理到性能优化:全面解析 Android Compose

文章目录 引言一、Android Compose基本概念1.1 什么是Android Compose?1.2 Compose的优势1.3 如何在项目中使用Compose 二、Compose中的状态管理2.1 状态管理的重要性2.2 Compose中的状态和数据流2.3 使用State和MutableState处理状态2.4 通过ViewModel进行状态管理 三、Compose中的列表和滚动

Android 10.0 mtk平板camera2横屏预览旋转90度横屏拍照图片旋转90度功能实现

1.前言 在10.0的系统rom定制化开发中,在进行一些平板等默认横屏的设备开发的过程中,需要在进入camera2的 时候,默认预览图像也是需要横屏显示的,在上一篇已经实现了横屏预览功能,然后发现横屏预览后,拍照保存的图片 依然是竖屏的,所以说同样需要将图片也保存为横屏图标了,所以就需要看下mtk的camera2的相关横屏保存图片功能, 如何实现实现横屏保存图片功能 如图所示: 2.mtk

android应用中res目录说明

Android应用的res目录是一个特殊的项目,该项目里存放了Android应用所用的全部资源,包括图片、字符串、颜色、尺寸、样式等,类似于web开发中的public目录,js、css、image、style。。。。 Android按照约定,将不同的资源放在不同的文件夹中,这样可以方便的让AAPT(即Android Asset Packaging Tool , 在SDK的build-tools目

Android fill_parent、match_parent、wrap_content三者的作用及区别

这三个属性都是用来适应视图的水平或者垂直大小,以视图的内容或尺寸为基础的布局,比精确的指定视图的范围更加方便。 1、fill_parent 设置一个视图的布局为fill_parent将强制性的使视图扩展至它父元素的大小 2、match_parent 和fill_parent一样,从字面上的意思match_parent更贴切一些,于是从2.2开始,两个属性都可以使用,但2.3版本以后的建议使

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

Android逆向(反调,脱壳,过ssl证书脚本)

文章目录 总结 基础Android基础工具 定位关键代码页面activity定位数据包参数定位堆栈追踪 编写反调脱壳好用的脚本过ssl证书校验抓包反调的脚本打印堆栈bilibili反调的脚本 总结 暑假做了两个月的Android逆向,记录一下自己学到的东西。对于app渗透有了一些思路。 这两个月主要做的是代码分析,对于分析完后的持久化等没有学习。主要是如何反编译源码,如何找到

android系统源码12 修改默认桌面壁纸--SRO方式

1、aosp12修改默认桌面壁纸 代码路径 :frameworks\base\core\res\res\drawable-nodpi 替换成自己的图片即可,不过需要覆盖所有目录下的图片。 由于是静态修改,则需要make一下,重新编译。 2、方法二Overlay方式 由于上述方法有很大缺点,修改多了之后容易遗忘自己修改哪些文件,为此我们采用另外一种方法,使用Overlay方式。