一、Presentation 介绍
要了解 API 的具体调用,推荐先查看官方的文档:Presentation文档
Android 从4.2开始支持双屏显示,开发时需 minSdkVersion >= 17 。Android 连接两个屏幕时,自动分配主屏和副屏,主屏显示正常的 Activity 界面,副屏通过创建 Presentation 类来实现。
通过查看 Presentation 继承关系可知,Presentation 继承自 Dialog,创建的时候需要遵循 Dialog 相关要求。当和 Presentation 相关联的屏幕被移除后,Presentation 也会自动的被移除,所以当 Activity 处于 pause 和 resume 的状态时,Presentation 也需要特别注意当前显示的内容的状态。
二、Presentation 实现
首先建立一个空工程,然后创建MyPresentation类继承Presentation类,由于我需要在副屏界面创建2个Button,并监听点击事件所以我implements了View.OnClickListener接口。
//MyPresentation.java
package com.example.multidisplaydomo;
import android.app.Presentation;
import android.content.Context;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyPresentation extends Presentation implements View.OnClickListener{
public MyPresentation(Context context, Display display){
super(context,display);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mypresentation);
Button mypresentation_button_1 = (Button) findViewById(R.id.mypresentation_button_1);
Button mypresentation_button_2 = (Button) findViewById(R.id.mypresentation_button_2);
mypresentation_button_1.setOnClickListener(this);
mypresentation_button_2.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.mypresentation_button_1:
Toast.makeText(getContext(),"mypresentation_button_1 onClick",Toast.LENGTH_SHORT).show();
break;
case R.id.mypresentation_button_2:
Toast.makeText(getContext(),"mypresentation_button_2 onClick",Toast.LENGTH_SHORT).show();
break;