安卓双屏异显Presentation

主页代码:

 override fun onCreate(savedInstanceState: Bundle?) {
        log("====MainActivity=====", "onCreate")
        super.onCreate(savedInstanceState)
        ...
        setContentView(contentView)
        addSecondScreen()
        ...
    }

添加副屏

 /**
     * 添加副屏
     */
    private fun addSecondScreen() {
        if (mPresentation == null) {
            val mDisplayManager =
                [email protected](Context.DISPLAY_SERVICE) as DisplayManager
            val displays = mDisplayManager.displays //得到显示器数组
            if (displays.size > 1) {
                mPresentation = DifferentDisplay(applicationContext, displays[1]) //displays[1]是副屏
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    mPresentation?.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY)
                } else {
                    mPresentation?.window?.setType(WindowManager.LayoutParams.TYPE_PHONE)
                }
            }
        }
        if (mPresentation?.isShowing == false) {
            try {
                mPresentation?.show()
                isPresentationOn = true
            } catch (e: Exception) {
                isPresentationOn = false
                e.printStackTrace()
                AppUtils.goToSettings(this)
                toast("请开启[允许出现在其他应用上]权限!")
                Process.killProcess(Process.myPid())
            }

        }
    }

添加权限

   /**
     * 去当前应用的设置页面开启[允许出现在其他应用上]权限的权限
     */
    fun goToSettings(context: Context?) {
        try {
            val intent = Intent(
                Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
                Uri.parse("package:" + context?.packageName)
            )
            context?.startActivity(intent)
        } catch (e: Exception) {
        }
    }

副屏页面

open class DifferentDisplay(context: Context, display: Display) : Presentation(context, display) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(SecondScreenView(context))
    }
}

ps:
单屏设备开启异显调试方式:开发者模式>>>模拟辅助显示设备>>>选择需要的分辨率

你可能感兴趣的:(安卓双屏异显Presentation)