Blog Infos
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.
A 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
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
References
Stay in touch
This article was previously published on proandroiddev.com