Blog Infos
Author
Published
Topics
,
Author
Published

 

Android 14 introduces a privacy-preserving screenshot detection API to create a more standardized screenshot detection experience.

Apps can register callbacks on a per-activity basis using this API.

If the user takes a screenshot while that activity is visible, these callbacks are invoked, and the user is notified.

screenshot can only be detected if the user presses a specific combination of hardware buttons.

Implementation
Add permission to Manifest
  • This is an install-time permission, so we do not need to worry about it during runtime 🙌
<uses-permission android:name="android.permission.DETECT_SCREEN_CAPTURE" />

Implement ScreenCaptureCallback interface

  • A method called onScreenCaptured() needs to be overridden.
@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
class MainActivity : ComponentActivity(), Activity.ScreenCaptureCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
             Text("Screenshot Detection API")    
        }
    }

    override fun onStart() {
        super.onStart()
        // Register callback to detect screenshot
        registerScreenCaptureCallback(mainExecutor, this)
    }

    override fun onStop() {
        super.onStop()
        // unregister callback to detect screenshot
        unregisterScreenCaptureCallback(this)
    }

    override fun onScreenCaptured() {
        // Inform the user or do whatever you want
        Log.d(TAG, "onScreenCaptured: Screenshot detected")
    }

    companion object {
        const val TAG = "MainActivity"
    }

    
}
Sample Code

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

REST in Peace: A Journey Through API Protection

Isn’t Droidcon a Mobile Developer’s conference? So, why would I care about protecting REST APIs? Well, think twice! API protection starts in the app, as the protection level of the API will be only as…
Watch Video

REST in Peace: A Journey Through API Protection

Andreas Luca & Marc Obrador Sureda
Head of Solution Integration & CTO , Co-founder
Build38

REST in Peace: A Journey Through API Protection

Andreas Luca & Mar ...
Head of Solution Int ...
Build38

REST in Peace: A Journey Through API Protection

Andreas Luca & M ...
Head of Solution Integrat ...
Build38

Jobs

Demo
  • As I do not have an Android 14 device, the following screenshot comes from the official documentation.

 

https://developer.android.com/static/about/versions/14/images/screenshot-detection.svg

 

This article was previously published on proandroiddev.com

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
This tutorial is the second part of the series. It’ll be focussed on developing…
READ MORE
blog
We recently faced a problem with our application getting updated and reaching slowly to…
READ MORE
blog
A few weeks ago I started with a simple question — how to work…
READ MORE
blog
One of the main functions of a mobile phone was to store contacts information.…
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