Use of Presentation and custom Dialog, presentationdialog

Source: Internet
Author: User

Use of Presentation and custom Dialog, presentationdialog

This article is an original article. For more information, see the source.

DEMO is included at the end of the article. Please download it from a friend you need.

Since API 17, Android has provided the Presentation class, providing official support for multi-screen display development. Of course, the implementation of the final requirement also requires support from underlying hardware and drivers.

Basic requirements:The full screen display of the secondary screen is different from that of the primary screen. When the content displayed in the full screen of the primary screen changes, the continuity of the content displayed on the secondary screen (for example, video playback on the secondary screen) does not affect.

The main content of this article includes the following:

(1) Basic Presentation usage;

(2) Use of custom Dialog (full screen );

(3) use the custom Dialog box instead of switching between activities to prevent the Presentation from being no longer displayed or the display content from continuing because the Activity enters the Paused status, for example, if you want the secondary screen to play video continuously, if you use Activity switching, the new Activity-controlled Presentation after Activity switching is difficult to continue the playing position of the previous Presentation video and the user experience is poor.

For advanced Presentation usage, such as hardware status change event listening, please refer to the official API or other articles.

Generally, Presentation has several key points:

(1) The Official Android API documentation contains the following paragraphs: "A presentation is automatically canceled (seecancel()) When the display to which it is attached is removed. an activity shoshould take care of pausing and resuming whatever content is playing within the presentation whenever the activity itself is paused or resumed. ", this means that when the Display bound to the presentation is removed or the Activitiey enters the pauseed or resuming status, we must control the life cycle of the Presentation by ourselves. That is, the Presentation created by the Activity when it is not displayed in the active Display. will disappear.

(2) obtain the Display. Each Presentation must specify a separate Display to Display different content from the main screen.

(3) set ContentView. Like Activity, Presentation can also use the setContentView method to set the XML layout file displayed by Presentation.

Note:To facilitate code refactoring and logical separation, the custom Presentation and Dialog are defined as separate classes. If the functions are simple, they can also be defined as internal classes.

1. Custom Presentation display

Generally, two Display methods are required for Presentation Display: MediaRouter and DisplayManager. In this article, DisplayManage is used to obtain Display.

(1) Create the layout file presentation_content.xml. The effect is as follows:

(2) Create the XiaoshubaoPresentation. java class file:

1 package xiaoshubao. presentationdemo; 2 3 import android. app. presentation; 4 import android. content. context; 5 import android. OS. bundle; 6 import android. view. display; 7 import android. widget. textView; 8 9/** 10 * Author: xiaoshubao11 * Time: 2016/06/3012 * version: V1.013 * Description: 14 */15 public class XiaoshubaoPresentation extends Presentation {16 private Display mdisplay; 17 public XiaoshubaoPresentation (Context context, Display display) {18 super (context, display); 19 this. mdisplay = display; 20} 21 22 23 @ Override24 protected void onStart () {25 super. onStart (); 26 MessageUtils. showInfo ("MyPresentation .... onStart "); 27} 28 29 @ Override30 protected void onStop () {31 super. onStop (); 32 MessageUtils. showInfo ("MyPresentation .... onStop "); 33} 34 35 @ Override36 public void show () {37 super. show (); 38 MessageUtils. showInfo ("MyPresentation .... show "); 39} 40 41 @ Override42 public void hide () {43 super. hide (); 44 MessageUtils. showInfo ("MyPresentation .... hide "); 45} 46 47 @ Override48 public void dismiss () {49 super. dismiss (); 50 MessageUtils. showInfo ("MyPresentation .... dismiss "); 51} 52 53 @ Override54 protected void onCreate (Bundle savedInstanceState) {55 super. onCreate (savedInstanceState); 56 setContentView (R. layout. presentation_content); 57 initData (); 58 MessageUtils. showInfo ("MyPresentation .... savedInstanceState "); 59} 60 61 private void initData () {62 TextView mPresentationId = (TextView) findViewById (R. id. presentationIdTextView); 63 mPresentationId. setText ("" + mdisplay. getDisplayId (); 64} 65}View Code

(3) display XiaoshubaoPresentation

1/** 2 * initialize secondary screen display 3 */4 private void initPresentation () {5 DisplayManager displayManager = (DisplayManager) getSystemService (Context. DISPLAY_SERVICE); 6 if (displayManager! = Null) {7 Display [] displays = displayManager. getDisplays (DisplayManager. DISPLAY_CATEGORY_PRESENTATION); 8 for (Display display: displays) {9 XiaoshubaoPresentation mPresentation = new XiaoshubaoPresentation (this, display); 10 mPresentation. show (); 11 mPresentationListView. add (mPresentation); 12} 13} 14 TextView presentationCount = (TextView) findViewById (R. id. presentationCount); 15 presentationCount. setText ("Number of secondary screens:" + mPresentationListView. size (); 16}View Code

Code Description:

  • DisplayManager displayManager = (DisplayManager) getSystemService (Context. DISPLAY_SERVICE); obtains the displayManager object to facilitate Display of abstract objects on the Display screen.
  • Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
    DISPLAY_CATEGORY_PRESENTATION:This category can be used to identify secondary displays that are suitable for
    * Use as presentation displays such as HDMI or Wireless displays to select the Display that can be used for Presentation Display.
  • XiaoshubaoPresentation mPresentation = new XiaoshubaoPresentation (this, display); // sets the Display of Presentation
  • MPresentation. show (); // display Presentation

 (4) Hide Presentation

  presentation.hide();

(5) Release Presentation Resources

  presentation.dismiss();

IICustom Dialog display

General idea: define Style to control the full screen display of Dialog-> define the Dialog XML layout file-> customize the Dialog class file-> respond to the Dialog interface control event.

(1) define the Style in full screen display of Dialog

1 <style name = "XiaoshubaoDialogStyle"> 2 <item name = "android: windowNoTitle"> true </item> 3 <item name = "android: windowFullscreen "> true </item> 4 </style>View Code

(2) Dialog layout file Effect

Dialog1 Effect

Dialog2 Effect

(3) create a XiaoshubaoFirstDialog. java class file

1 package xiaoshubao. presentationdemo; 2 3 import android. app. dialog; 4 import android. content. context; 5 import android. view. view; 6 import android. widget. button; 7 8/** 9 * Author: xiaoshubao10 * Time: 2016/06/3011 * version: V1.012 * Description: 13 */14 public class XiaoshubaoFirstDialog extends Dialog {15 private Context context Context; 16 private XiaoshubaoFirstDialog dialog; 17 18 public XiaoshubaoFirstDialog (Context context, int themeResId) {19 super (context, themeResId); 20 this. context = context; 21 dialog = this; 22} 23 24 @ Override25 public void create () {26 super. create (); 27} 28 29 @ Override30 public void show () {31 super. show (); 32 initListener (); 33} 34 35/** 36 * initialization control event 37 */38 private void initListener () {39 Button createSecondDialogButton = (Button) findViewById (R. id. createSecondDialogButton); 40 createSecondDialogButton. setOnClickListener (btn_createseconddiener); 41 42 Button dismmisDialogButton = (Button) findViewById (R. id. dismmisDialogButton); 43 dismmisDialogButton. setOnClickListener (dismmisDialogOnclick); 44} 45 46 private View. onClickListener btn_createSecondDialog = new View. onClickListener () {47 @ Override48 public void onClick (View v) {49 XiaoshubaoSecondDialog secondDialog = new XiaoshubaoSecondDialog (context, R. style. xiaoshubaoDialogStyle); 50 secondDialog. setContentView (R. layout. dialog_second); 51 secondDialog. show (); 52} 53}; 54 private View. onClickListener dismmisDialogOnclick = new View. onClickListener () {55 @ Override56 public void onClick (View v) {57 dialog. dismiss (); 58} 59}; 60}View Code

(4) create a XiaoshubaoSecondDialog. java class file

1 package xiaoshubao. presentationdemo; 2 3 import android. app. dialog; 4 import android. content. context; 5 import android. view. view; 6 import android. widget. button; 7 8/** 9 * Author: xiaoshubao10 * Time: 2016/06/3011 * version: V1.012 * description: 13 */14 public class XiaoshubaoSecondDialog extends Dialog {15 public XiaoshubaoSecondDialog (Context context, int themeResId) {16 super (context, themeResId); 17 dialog = this; 18} 19 private XiaoshubaoSecondDialog dialog; 20 @ Override21 public void create () {22 super. create (); 23} 24 25 @ Override26 public void show () {27 super. show (); 28 initListener (); 29} 30/** 31 * initialization control event 32 */33 private void initListener () {34 Button dismmisDialogButton = (Button) findViewById (R. id. dismmisDialogButton); 35 dismmisDialogButton. setOnClickListener (dismmisDialogOnclick); 36} 37 private View. onClickListener dismmisDialogOnclick = new View. onClickListener () {38 @ Override39 public void onClick (View v) {40 dialog. dismiss (); 41} 42}; 43}View Code

(5) set the Dialog control to respond to events

1/** 2 * initialize the control event 3 */4 private void initListener () {5 Button createSecondDialogButton = (Button) findViewById (R. id. createSecondDialogButton); 6 createSecondDialogButton. setOnClickListener (btn_createseconddiener); 7 8 Button dismmisDialogButton = (Button) findViewById (R. id. dismmisDialogButton); 9 dismmisDialogButton. setOnClickListener (dismmisDialogOnclick); 10} 11 12 private View. onClickListener btn_createSecondDialog = new View. onClickListener () {13 @ Override14 public void onClick (View v) {15 XiaoshubaoSecondDialog secondDialog = new XiaoshubaoSecondDialog (context, R. style. xiaoshubaoDialogStyle); 16 secondDialog. setContentView (R. layout. dialog_second); 17 secondDialog. show (); 18} 19}; 20 private View. onClickListener dismmisDialogOnclick = new View. onClickListener () {21 @ Override22 public void onClick (View v) {23 dialog. dismiss (); 24} 25 };View Code

(6) display Dialog

1 public void btn_createDialogBtnClick (View v) {2 mainDialog = new XiaoshubaoFirstDialog (this, R. style. xiaoshubaoDialogStyle); // sets the full screen Style 3 mainDialog. setContentView (R. layout. dialog_first); // set the layout of dialog 4 mainDialog. show (); // display the dialog interface 5}View Code

(7) Release Dialog Resources

Dialog. dismiss ();
Iii. Summary
The project's final display effect is shown in the previous preview. The focus is to show new Android users multi-screen and Dialog full screen display and Event Response.
DEMO

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.