To implement this, you merely set the flag on the window of your exercise like so:
import android.view.WindowManager
.window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)
1. Disable Display Seize in Software Stage
If you wish to disable display seize for all the app, making use of the flag in Software class is best choice.
In your Software class, register the exercise life cycle callback and apply the safe flag in onActivityCreated() methodology. This manner at any time when an exercise is created, the safe flag might be utilized routinely.
bundle information.androidhive.screen_capture
import android.app.Exercise
import android.app.Software
import android.os.Bundle
import android.view.WindowManager
class MyApplication : Software() {
override enjoyable onCreate() {
tremendous.onCreate()
registerActivityLifeCycle()
}
non-public enjoyable registerActivityLifeCycle() {
registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
override enjoyable onActivityCreated(exercise: Exercise, savedInstanceState: Bundle?) {
disableScreenCapture(exercise)
}
override enjoyable onActivityStarted(exercise: Exercise) {}
override enjoyable onActivityResumed(exercise: Exercise) {}
override enjoyable onActivityPaused(exercise: Exercise) {}
override enjoyable onActivityStopped(exercise: Exercise) {}
override enjoyable onActivitySaveInstanceState(exercise: Exercise, outState: Bundle) {}
override enjoyable onActivityDestroyed(exercise: Exercise) {}
})
}
non-public enjoyable disableScreenCapture(exercise: Exercise) {
exercise.window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)
}
}
Remember so as to add the applying class to your <software> tag in AndroidManifest.xml
<?xml model=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:instruments=”http://schemas.android.com/instruments”>
<software
android:title=”.MyApplication”
…
2. Disable Display Seize solely in sure Actions
If you wish to disable display seize in solely chosen actions, it’s good to apply the flag in these exercise solely. You are able to do this in onCreate() or onResume() methodology.
class MainActivity : AppCompatActivity() {
override enjoyable onCreate(savedInstanceState: Bundle?) {
tremendous.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.structure.activity_main)
}
override enjoyable onResume() {
tremendous.onResume()
disableScreenCapture()
}
non-public enjoyable disableScreenCapture() {
window.setFlags(
WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE
)
}
}
3. Permitting Display Catpture
As soon as this flag is utilized, if you wish to permit display seize once more, the safe flag will be eliminated utilizing clearFlags() methodology.
class MainActivity : AppCompatActivity() {
override enjoyable onCreate(savedInstanceState: Bundle?) {
tremendous.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.structure.activity_main)
}
override enjoyable onResume() {
tremendous.onResume()
enableScreenCapture()
}
non-public enjoyable enableScreenCapture(){
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
}
}