通过源码可得知 public class Presentation extends Dialog,Presentation继承于dialog,依托于Activity存在。
新建一个Presentation
public class DifferentDislay extends Presentation {
public static CameraPreview cameraView2;
private Context mContext;
private Activity mActivity;
public DifferentDislay(Activity activity, Context context, Display display) {
super(context,display);
mContext=context;
mActivity=activity;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
}
private void initView(){
cameraView2 = (CameraPreview) findViewById(R.id.cameraView2);
}
public void close() {
onDisplayRemoved();
dismiss();
}
@Override
protected void onStart() {
super.onStart();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onDisplayRemoved() {
super.onDisplayRemoved();
}
}
新建Activity
public class MainActivity extends AppCompatActivity {
DifferentDislay differentDislay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
permisss();
initDisplay();
}
private void permisss(){
if (Build.VERSION.SDK_INT >= 23) {
if (!Settings.canDrawOverlays(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, 1);
}
}
}
/**
* displays[0] 主屏
* displays[1] 副屏
*/
private void initDisplay(){
DisplayManager manager = (DisplayManager)
getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = manager.getDisplays();
if (displays.length<2){
return;
}
differentDislay = new DifferentDislay(this,this,displays[1]);
differentDislay.getWindow().setType(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
differentDislay.show();
}
public void closeAdvertDisplay(){
if(differentDislay!=null) {
differentDislay.close();
differentDislay = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
closeAdvertDisplay();
}
}
没错,就是这么简单!
————————————————
原文链接:https://blog.csdn.net/guodashen007/article/details/124444980