参考资料
链接: Android实现双屏异显. 链接: Android 6.0:Unable to add window android.view.ViewRootImpl$W@5e2d85a – permission denied. 公司项目后期需要设备支持双屏异显,家境贫寒的我表示压根没听过,赶紧码起来,自己建了一个小demo。
初识Presentation
我发现了Presentation,安卓官方对于Presentation是这样描述的 大致意思就是,是一种特殊的对话框,是为了在辅助显示器上演示内容。
快速上手
想要使用Presentation,首先得找到自己的辅助屏幕,这里我们使用了安卓官方推荐的第二种方式 创建一个activity,然后在里面写一个内部类继承于Presentation,当然我只是图方便,实际上肯定是要新建一个的
private class DifferentDislay extends Presentation {
public DifferentDislay(Context outerContext, Display display) {
super(outerContext, display);
}
public DifferentDislay(Context outerContext, Display display, int theme) {
super(outerContext, display, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diffrentdisplay);
}
}
然后开始简单快乐的找副屏,找到过后显示出来
public class MainActivity extends AppCompatActivity {
DisplayManager mDisplayManager;
Display[] displays;
public static int OVERLAY_PERMISSION_REQ_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPresentation();
}
private void initPresentation() {
mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
displays = mDisplayManager.getDisplays();
DifferentDislay mPresentation = new DifferentDislay(this
, displays[1]);
mPresentation.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mPresentation.show();
}
然后就开始血泪填坑史,运行过后果断的闪退了,一看报错,哦,没有加权限嘛,小事,在AndroidManifest.xml中加上权限
<!-- 显示系统窗口权限 -->
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
ok,再次运行,果断又闪退,等等,我的设备是安卓7.0的,要动态申请,那就申请一个呗。可是我发现我的动态申请权限代码不起作用,最后发现这个权限要跳到单独的页面申请,完全代码如下:
成品代码请君品尝
public class MainActivity extends AppCompatActivity {
DisplayManager mDisplayManager;
Display[] displays;
public static int OVERLAY_PERMISSION_REQ_CODE = 99;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestDrawOverLays();
}
private void initPresentation() {
mDisplayManager = (DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE);
displays = mDisplayManager.getDisplays();
DifferentDislay mPresentation = new DifferentDislay(this
, displays[1]);
mPresentation.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
mPresentation.show();
}
private class DifferentDislay extends Presentation {
public DifferentDislay(Context outerContext, Display display) {
super(outerContext, display);
}
public DifferentDislay(Context outerContext, Display display, int theme) {
super(outerContext, display, theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diffrentdisplay);
}
}
@TargetApi(Build.VERSION_CODES.M)
public void requestDrawOverLays() {
if (!Settings.canDrawOverlays(MainActivity.this)) {
Toast.makeText(this, "您还没有打开悬浮窗权限", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + MainActivity.this.getPackageName()));
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
} else {
initPresentation();
}
}
@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (!Settings.canDrawOverlays(this)) {
Toast.makeText(this, "授权失败", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "授权成功", Toast.LENGTH_SHORT).show();
initPresentation();
}
}
}
}
这样,一个简单的双屏异显demo就完成啦,共勉,别忘记加权限哦!
|