背景:
车载操作系统,现在的折叠屏手机, 已经售货机等,都会需要有多屏异显的功能
具体代码实现
添加显示到第二屏需要的代码
package com.mine.myapplication;
import android.app.Presentation;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class DifferentDislay extends Presentation {
public DifferentDislay(Context outerContext, Display display) {
super(outerContext,display);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diffrentdisplay);
Button btn = (Button)this.findViewById(R.id.button);
TextView tv = (TextView) this.findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tv.setText("hello different display");
}
});
}
}
第二屏显示需要额外的权限
添加mainfest 权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
该显示的权限需要显式的进行声明
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(intent);
}
调用代码显示第二屏的内容
DisplayManager mDisplayManager = (DisplayManager)MainActivity.this.getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = mDisplayManager.getDisplays();
DifferentDislay mPresentation =new DifferentDislay (getApplicationContext(), displays[1]);
mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
mPresentation.show();
测试
启动模拟器, 配置模拟器的第二个屏幕
程序运行效果
参考:
https://blog.csdn.net/hello_1995/article/details/120739601 https://blog.csdn.net/u014361280/article/details/115539355
包含具体的android的实现的代码参考
https://blog.csdn.net/Jason_Lee155/article/details/116465425
|