How to prevent Screen Capture in Android

To avoid screen capture in an Android apps, you can use the FLAG_SECURE flag to prevent the screenshots and screen recording of app. When this flag is set, Android prevents the content of the app from appearing in screenshots, screen recording and recent app thumbnail.

To implement this, you simply set the flag on the window of your activity like so:

import android.view.WindowManager

.window.setFlags(
	WindowManager.LayoutParams.FLAG_SECURE,
	WindowManager.LayoutParams.FLAG_SECURE
)
    

1. Disable Screen Capture in Application Level

If you want to disable screen capture for the entire app, applying the flag in Application class is best option.
  1. In your Application class, register the activity life cycle callback and apply the secure flag in onActivityCreated() method. This way whenever an activity is created, the secure flag will be applied automatically.
    
    package info.androidhive.screen_capture
    
    import android.app.Activity
    import android.app.Application
    import android.os.Bundle
    import android.view.WindowManager
    
    class MyApplication : Application() {
        override fun onCreate() {
            super.onCreate()
    
            registerActivityLifeCycle()
        }
    
        private fun registerActivityLifeCycle() {
            registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
                override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                    disableScreenCapture(activity)
                }
    
                override fun onActivityStarted(activity: Activity) {}
                override fun onActivityResumed(activity: Activity) {}
                override fun onActivityPaused(activity: Activity) {}
                override fun onActivityStopped(activity: Activity) {}
                override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
                override fun onActivityDestroyed(activity: Activity) {}
            })
        }
    
        private fun disableScreenCapture(activity: Activity) {
            activity.window.setFlags(
                WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE
            )
        }
    }
  2. Don't forget to add the application class to your <application> tag in AndroidManifest.xml
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <application
            android:name=".MyApplication"
            ...
    
        

2. Disable Screen Capture only in certain Activities

If you want to disable screen capture in only selected activities, you need to apply the flag in those activity only. You can do this in onCreate() or onResume() method.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        disableScreenCapture()
    }

    private fun disableScreenCapture() {
        window.setFlags(
            WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE
        )
    }
}
    

3. Allowing Screen Catpture

Once this flag is applied, if you want to allow screen capture again, the secure flag can be removed using clearFlags() method.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        enableScreenCapture()
    }

    private fun enableScreenCapture(){
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
}

    
Previous Post Next Post

نموذج الاتصال