一、封装成Modifier
/** * 当前界面不允许截屏,退出该界面后恢复 */ @Composable fun Modifier.secureScreen(enable: Boolean = true): Modifier = composed { val context = LocalContext.current DisposableEffect(Unit) { if (enable) { (context as? Activity)?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE) } else { (context as? Activity)?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } onDispose { (context as? Activity)?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } } this }二、封装成可组合项
/** * 当前界面不允许截屏,退出该界面后恢复 */ @Composable fun SecureScreen(enable: Boolean = true) { val context = LocalContext.current DisposableEffect(Unit) { if (enable) { (context as? Activity)?.window?.addFlags(WindowManager.LayoutParams.FLAG_SECURE) } else { (context as? Activity)?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } onDispose { (context as? Activity)?.window?.clearFlags(WindowManager.LayoutParams.FLAG_SECURE) } } }