How to prevent Screenshot / Screen Capture in Android JAVA & Kotlin ?

If you do not want to allow people to take screenshots of some data from your android application like proprietary photos or articles etc, then android provides the secure mechanism using which you can do the same.

When you are developing your application, you will need to modify the onCreate function of your android activity as mentioned below to add “FLAG_SECURE” flag for WindowManager.LayoutParams . As per Android website, FLAG_SECURE treats the content of the window as secure, preventing it from appearing in screenshots or from being viewed on non-secure displays

For Kotlin, modify your MainActivity.kt as,

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);
}

For Android JAVA, modify your MainActivity.java as,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                           WindowManager.LayoutParams.FLAG_SECURE);
}

Now, once you modify the code, compile and use this application and tries to take screenshots as mentioned in our another post “How to Capture Screenshot in android using adb ?” you should see the system dialog as below,

Related :   adb reboot - Restart Android without Power Button using ADB

couldn’t save screenshot, Taking screenshots isn’t allowed by the app or your organisation


lynxbee_ezoic

Subscribe with Valid Email Id to receive updates in Inbox. ( Secured by Google FeedBurner )

   


Leave a Comment

Android Android Applications Android Build system Android Commands Application Libraries Application Stack / User Interface Bash / Shell Scripts Bluetooth driver Cloud Technologies Commands and Packages Compilation Content Management System Core Kernel C Programs Development & Build Development, Debugging and Performance Tools Development Environment Setup Django & REST Api Errors & Failures Git Hardware Platforms HTML JAVA Programs Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Multimedia Audio, Video, Images NDK / Middleware / HAL OS Concepts PHP Programming Languages Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Source Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Web design and development Website Hosting Wordpress Yocto / Bitbake / Openembedded