这个demo主要演示presentation的用法,presentation是一种特殊的对话框,主要用于在另外一块屏幕上显示内容。值得注意的是在创建presentation前必须把presentation与它的目标屏幕相关联。所以在使用它之前必须为它选择一个Display。
选择display的方法主要有两种
一是利用MediaRouter
二是利用DisplayManager,
本例演示的是后一种方法
创建主布局文件:activity_main.xml
主要是一个用于控制显示所有屏幕的复选框和一个显示所有屏幕的列表框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=" Tish demostrates how to use a presentation and the DisplayManger to show content on other display
try connecting a second display and watch what happenes selecting a display and will open a presentatio on it."/>
<CheckBox
android:id="@+id/show_all_presentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示所有pressentation"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
编写MainActivity
public class MainActivity extends Activity implements OnCheckedChangeListener {
private static final String PRESENTATION_KEY = "presentation";
private DisplayManager displayManager;
private CheckBox show_all_displays;
private DisplayListAdapter displayAdapter;
private ListView listView;
// 定义存储photo的数组
private int[] photos = new int[] { R.drawable.guild1, R.drawable.guild2,
R.drawable.guild3, R.drawable.ic_launcher, R.drawable.puzzle,
R.drawable.test };
// 记录下一张照片的Id
private int nextPhotoNumber = 0;
/*
* 利用SparseArray存储所有屏幕的prsentation上显示的内容(尤其是当屏幕不止一个时数组的作用更大)
* SparseArray:稀疏数组,在存储数据不多的情况下可以大大地节省空间,此时可以代替hasmap使用
*/
SparseArray<PresentationContents> mSavedPresentationContents;
// 根据displayId记录当前显示的presentation
SparseArray<DemoPresentation> mActivePresentations = new SparseArray<DemoPresentation>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 首先检查是否存在需要恢复的状态信息
if (savedInstanceState != null) {
mSavedPresentationContents = savedInstanceState
.getSparseParcelableArray(PRESENTATION_KEY);
} else {
mSavedPresentationContents = new SparseArray<PresentationContents>();
}
displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
show_all_displays = (CheckBox) findViewById(R.id.show_all_presentation);
show_all_displays.setOnCheckedChangeListener(this);
listView = (ListView) findViewById(R.id.listView);
displayAdapter = new DisplayListAdapter(this);
listView.setAdapter(displayAdapter);
}
/**
* 定义listView的适配器,用于显示所有的屏幕信息
*
* @author aaaa
*
*/
private class