Blankj / Blankj/AndroidUtilCode

Toggling FLAG_SECURE in WindowManager to prevent taking screenshots

Open
#1,564 2 comments 0 reactions 1 assignee Claimed by @Blankj View on GitHub
help wanted
Dominant language
Java
Stars
33.6k
Forks
10.6k
PR merge metrics
No merged PRs in 30d

Description

## Describe the feature
Toggle [FLAG_SECURE](https://developer.android.com/reference/android/view/WindowManager.LayoutParams#FLAG_SECURE) to prevent screenshot form activity/class/base/app.

The FLAG_SECURE "flag" in WindowManager prevents taking a screenshot from the app's screen in Android versions above 7, and scrambles the snapped shot final image in Android versions lower than 7.

https://developer.android.com/reference/android/view/WindowManager.LayoutParams#FLAG_SECURE

## Reference
You can achieve it with only one line of code (Java):
`getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
`
Kotlin:
`window.setFlags( WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE)`

And to disable it (Java):
`getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
`
Kotlin:
`window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.