ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Screen recording detection-Android 15

--

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 callbackWindowManager

  • We will use WindowManager.addScreenRecordingCallback() , it takes 2 non-nullable parameters:
  1. Executor,
  2. 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.

That’s all for today, I hope you learned something new

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Nav Singh

Google Developer Expert for Android | Mobile Software Engineer at Manulife | Organizer at GDG Montreal

Responses (1)