Détecter quand les utilisateurs prennent des captures d'écran de l'appareil
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Image 1. Exemple de toast fourni par le système qui s'affiche lorsque l'utilisateur prend une capture d'écran d'une application compatible avec l'API de détection de capture d'écran.
Pour créer une expérience plus standardisée, Android 14 introduit une API de détection des captures d'écran protégeant la confidentialité. Cette API permet aux applications d'enregistrer des rappels par activité. Ces rappels sont appelés, et l'utilisateur en est informé lorsqu'une capture d'écran est prise et que cette activité est visible.
Cas d'utilisation compatibles
Dans Android 14, l'API système ne détecte une capture d'écran que si l'utilisateur effectue une combinaison spécifique sur les boutons physiques. L'API ne détecte pas les captures d'écran effectuées lors de l'exécution de commandes de test liées aux captures d'écran, y compris ADB, ou dans les tests d'instrumentation qui capturent les contenus actuels de l'écran de l'appareil.
Procédure d'implémentation
Pour ajouter la détection de captures d'écran, déclarez la nouvelle autorisation au moment de l'installation, DETECT_SCREEN_CAPTURE :
Ensuite, procédez comme suit pour chaque activité de votre application dans lesquelles les utilisateurs peuvent effectuer des captures d'écran :
Implémentez un rappel en remplaçant la fonction onScreenCapture(). Dans ce rappel, votre application peut prendre des mesures, par exemple avertir un autre utilisateur que quelqu'un a pris une capture d'écran d'une conversation par messages.
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.}};
Dans la méthode onStart() de l'activité, enregistrez le rappel de la capture d'écran.
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);}
Dans la méthode onStop() de l'activité, annulez le rappel de capture d'écran :
Contrôler la possibilité de faire des captures d'écran
Si vous ne souhaitez pas que le contenu de l'activité d'une application apparaisse sur des captures d'écran ou sur des écrans non sécurisés, définissez l'indicateur d'affichage FLAG_SECURE.
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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```"]]