Blog Infos
Author
Published
Topics
, , , ,
Author
Published

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 takes 2 non-nullable parameters:
  1. Executor,
  2. Consumer<Integer>

Job Offers

Job Offers


    Senior Android Engineer

    Carly Solutions GmbH
    Munich
    • Full Time
    apply now

    Senior Android Developer

    SumUp
    Berlin
    • Full Time
    apply now

OUR VIDEO RECOMMENDATION

Jobs

@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
Stay in touch

https://www.linkedin.com/in/navczydev/

 

https://github.com/navczydev?source=post_page—–26ee709b66b4——————————–

https://twitter.com/navczydev?source=post_page—–26ee709b66b4——————————–

References

https://developer.android.com/about/versions/15/features?source=post_page—–26ee709b66b4——————————–#screen-recording-detection

https://android-developers.googleblog.com/2024/06/the-third-beta-of-android-15.html?source=post_page—–26ee709b66b4——————————–

This article is previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Using annotations in Kotlin has some nuances that are useful to know
READ MORE
blog
One of the latest trends in UI design is blurring the background content behind the foreground elements. This creates a sense of depth, transparency, and focus,…
READ MORE
blog
Now that Android Studio Iguana is out and stable, I wanted to write about…
READ MORE
blog
The suspension capability is the most essential feature upon which all other Kotlin Coroutines…
READ MORE

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu