Detecting Screen Recordings
warning
Android 15 (API Level 35) is still in Beta 1 phase. Some changes and APIs might not be final and are still subject to change.
Android 15 introduces a way to detect a screen recording performed by user.
Basic Usage
First, you need to declare DETECT_SCREEN_RECORDING
install-time permission inside your AndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!-- Add this permission: -->
<uses-permission android:name="android.permission.DETECT_SCREEN_RECORDING" />
</manifest>
To start detecting screen recordings, you need to utilize these 2 key components within your Activity:
addScreenRecordingCallback
: This method registers your screen recording detection callback.removeScreenRecordingCallback
: This method unregisters your screen recording detection callback.
Here is a simple example of such Activity:
MainActivity.kt
package com.hanmajid.androidnotebook
import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.WindowManager.SCREEN_RECORDING_STATE_VISIBLE
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import com.hanmajid.androidnotebook.ui.theme.AndroidNotebookTheme
import java.util.function.Consumer
class MainActivity : ComponentActivity() {
private val screenRecordCallback = Consumer<Int> { state ->
if (state == SCREEN_RECORDING_STATE_VISIBLE) {
Log.i("TAG", "✅ Recording!")
} else {
Log.i("TAG", "⛔️ Not recording!")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AndroidNotebookTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Column {
Text("Hello Android Notebook")
}
}
}
}
}
override fun onStart() {
super.onStart()
val initialState = windowManager.addScreenRecordingCallback(mainExecutor, screenRecordCallback)
screenRecordCallback.accept(initialState)
}
override fun onStop() {
super.onStop()
windowManager.removeScreenRecordingCallback(screenRecordCallback)
}
}
The callback will be invoked whenever the screen recording state changes. There are two possible screen recording state values:
SCREEN_RECORDING_STATE_NOT_VISIBLE
: Indicates that the app that registered the callback is not visible in screen recording.SCREEN_RECORDING_STATE_VISIBLE
: Indicates that the app that registered the callback is visible in screen recording.