Screen recording detection-Android 15
Published in
2 min readJun 19, 2024
Android 15 offers several privacy-protecting features. We will discuss one of these in this article: Screen recording detection ⏺
A 4-step process to ensure everything works as expected.
1️⃣. — Add the install-time permission to your app’s AndroidManifest.xml
file
<uses-permission android:name="android.permission.DETECT_SCREEN_RECORDING" />
2️⃣. — Define the screen-recording callback
private val screenRecordingCallback = { state: Int ->
when (state) {
SCREEN_RECORDING_STATE_VISIBLE -> {
// We're being recorded - Inform the user
}
else -> {
// We're not being recorded
}
}
}
3️⃣. — Add callback — WindowManager
- We will use
WindowManager.addScreenRecordingCallback()
, it takes2 non-nullable
parameters:
- Executor,
- Consumer<Integer>
@RequiresApi(35)
override fun onStart() {
//...
val initialState =
windowManager.addScreenRecordingCallback(
mainExecutor,
screenRecordingCallback
)
Log.d("MainActivity", "onStart: Initial state: $initialState")
// Trigger the callback with the initial state
screenRecordingCallback.invoke(initialState)
}
4️⃣. — Remove the callback — WindowManager
- We must remove the callback when our
activity’s onStop method
is called to ensure that we only receive a callback when the activity is in the active state WindowManager.removeScreenRecordingCallback()
method is used to remove the callback. It takes a single non-nullable parameter:Consumer<Integer>
/** Stop monitoring for screen recording detection of the activity */
@RequiresApi(35)
override fun onStop() {
// ...
windowManager.removeScreenRecordingCallback(screenRecordingCallback)
}
As it is per-activity, we need to do the above steps 👆 for each
activity
to add it.