Rilevare quando gli utenti acquisiscono screenshot del dispositivo
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Figura 1. Un esempio del messaggio di notifica fornito dal sistema
che viene visualizzato quando l'utente acquisisce uno screenshot di un'app che supporta l'API
di rilevamento degli screenshot.
Per creare un'esperienza più standardizzata per il rilevamento degli screenshot,
Android 14 introduce un'API per il rilevamento degli screenshot che tutela la privacy. Questa API consente alle app di registrare i callback in base all'attività. Questi
callback vengono richiamati e l'utente riceve una notifica quando acquisisce uno
screenshot mentre l'attività è visibile.
Casi d'uso supportati
In Android 14, l'API di sistema rileva uno screenshot solo se l'utente esegue una
combinazione specifica di pressioni dei tasti hardware. L'API non rileva
gli screenshot acquisiti durante l'esecuzione di comandi di test correlati agli screenshot,
inclusi ADB o all'interno di test di strumentazione che acquisiscono i contenuti
dello schermo corrente del dispositivo.
Passaggi per l'implementazione
Per aggiungere il rilevamento degli screenshot, dichiara la nuova autorizzazione DETECT_SCREEN_CAPTURE
al momento dell'installazione:
Quindi, completa questi passaggi per ogni attività della tua app in cui gli utenti potrebbero
acquisire screenshot:
Implementa un callback eseguendo l'override della funzione onScreenCapture(). In questo
callback, la tua app può intraprendere azioni, ad esempio avvisare un altro utente che
qualcuno ha scattato uno screenshot di una conversazione di messaggistica.
Kotlin
valscreenCaptureCallback=Activity.ScreenCaptureCallback{// Add logic to take action in your app.}
Java
finalActivity.ScreenCaptureCallbackscreenCaptureCallback=newActivity.ScreenCaptureCallback(){@OverridepublicvoidonScreenCaptured(){// Add logic to take action in your app.}};
Nel metodo onStart() dell'attività, registra il callback dello screenshot.
Kotlin
overridefunonStart(){super.onStart()// Pass in the callback created in the previous step // and the intended callback executor (e.g. Activity's mainExecutor).registerScreenCaptureCallback(mainExecutor,screenCaptureCallback)}
Java
@OverrideprotectedvoidonStart(){super.onStart();// Pass in the callback created in the previous step // and the intended callback executor (e.g. Activity's mainExecutor).registerScreenCaptureCallback(executor,screenCaptureCallback);}
Nel metodo onStop() dell'attività, annulla la registrazione del callback dello screenshot:
Controllare la possibilità di acquisire screenshot
Se non vuoi che i contenuti dell'attività di un'app vengano visualizzati negli screenshot o su display non sicuri, imposta il flag di visualizzazione FLAG_SECURE.
I campioni di contenuti e codice in questa pagina sono soggetti alle licenze descritte nella Licenza per i contenuti. Java e OpenJDK sono marchi o marchi registrati di Oracle e/o delle sue società consociate.
Ultimo aggiornamento 2025-08-27 UTC.
[null,null,["Ultimo aggiornamento 2025-08-27 UTC."],[],[],null,["**Figure 1.** An example of the system-provided toast message that appears when the user takes a screenshot of an app that supports the screenshot detection API.\n\nTo create a more-standardized experience for detecting screenshots,\nAndroid 14 introduces a privacy-preserving screenshot detection\nAPI. This API lets apps register callbacks on a per-activity basis. These\ncallbacks are invoked, and the user is notified, when the user takes a\nscreenshot while that activity is visible.\n| **Note:** The callback doesn't provide an image of the actual screenshot. It's up to your app to determine what appeared on the screen when the user took a screenshot.\n\nSupported use cases\n\nIn Android 14, the system API only detects a screenshot if the user performs a\nspecific combination of hardware button presses. The API doesn't detect\nscreenshots that are taken when running test commands related to screenshots,\nincluding [ADB](/studio/command-line/adb), or within instrumentation tests that [capture the device's\ncurrent screen contents](/reference/androidx/test/core/app/DeviceCapture).\n\nImplementation steps\n\nTo add screenshot detection, declare the new [`DETECT_SCREEN_CAPTURE`](/reference/android/Manifest.permission#DETECT_SCREEN_CAPTURE)\ninstall-time permission: \n\n \u003cuses-permission android:name=\"android.permission.DETECT_SCREEN_CAPTURE\" /\u003e\n\nThen, complete these steps for each activity in your app where users might\ncapture screenshots:\n\n1. Implement a callback by overriding the `onScreenCapture()` function. In this\n callback, your app can take action, such as warning another user that\n someone took a screenshot of a messaging conversation.\n\n Kotlin \n\n ```kotlin\n val screenCaptureCallback = Activity.ScreenCaptureCallback {\n // Add logic to take action in your app.\n }\n ```\n\n Java \n\n ```java\n final Activity.ScreenCaptureCallback screenCaptureCallback =\n new Activity.ScreenCaptureCallback() {\n @Override\n public void onScreenCaptured() {\n // Add logic to take action in your app.\n }\n };\n ```\n2. In the activity's `onStart()` method, register the screenshot callback.\n\n **Note:** Given that a notice is shown with every screenshot detection signal, developers should provide in-context notices to the user when they are starting an activity that uses screenshot detection APIs so the system notice does not come as a surprise to users. \n\n Kotlin \n\n ```kotlin\n override fun onStart() {\n super.onStart()\n // Pass in the callback created in the previous step \n // and the intended callback executor (e.g. Activity's mainExecutor).\n registerScreenCaptureCallback(mainExecutor, screenCaptureCallback)\n }\n ```\n\n Java \n\n ```java\n @Override\n protected void onStart() {\n super.onStart();\n // Pass in the callback created in the previous step \n // and the intended callback executor (e.g. Activity's mainExecutor).\n registerScreenCaptureCallback(executor, screenCaptureCallback);\n }\n ```\n3. In the activity's `onStop()` method, unregister the screenshot callback:\n\n Kotlin \n\n ```kotlin\n override fun onStop() {\n super.onStop()\n unregisterScreenCaptureCallback(screenCaptureCallback)\n }\n ```\n\n Java \n\n ```java\n @Override\n protected void onStop() {\n super.onStop();\n unregisterScreenCaptureCallback(screenCaptureCallback);\n }\n ```\n\nControl ability to capture screenshots\n\nIf you don't want the contents of an app's activity to appear in screenshots, or\non non-secure displays, set the [`FLAG_SECURE`](/reference/android/view/Display#FLAG_SECURE) display flag.\n**Note:** To provide transparency and user control, consider adding a setting in your app that allows users to toggle this flag. \n\nKotlin \n\n```kotlin\nactivity.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE)\n```\n\nJava \n\n```java\nactivity.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);\n```"]]