Detectar quando usuários fazem capturas de tela no dispositivo
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Figura 1. Exemplo de mensagem de aviso do sistema
que aparece quando o usuário faz uma captura de tela de um app com suporte à
API de detecção de capturas de tela.
Para criar uma experiência mais padronizada para detectar capturas de tela,
o Android 14 apresenta uma API de detecção de capturas de tela
que preserva a privacidade. Ela permite que os apps registrem callbacks por atividade. Esses
callbacks são invocados, e o usuário é notificado quando faz uma
captura de tela enquanto a atividade está visível.
Casos de uso com suporte
No Android 14, a API do sistema só detecta uma captura de tela quando o usuário pressiona uma
combinação específica de botões físicos. A API não detecta
capturas de tela que são feitas com comandos de teste relacionados a elas,
incluindo testes com o adb ou testes de instrumentação que capturam o conteúdo
atual da tela do dispositivo.
Etapas de implementação
Para adicionar a detecção de capturas de tela, declare a nova permissão de instalação
DETECT_SCREEN_CAPTURE:
Em seguida, conclua estas etapas para cada atividade no app em que os usuários podem
fazer capturas de tela:
Implemente um callback substituindo a função onScreenCapture(). Nesse
callback, seu app pode realizar ações, como avisar outro usuário de que
alguém fez uma captura de tela de uma conversa por mensagens.
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.}};
No método onStart() da atividade, registre o callback da captura de tela.
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);}
No método onStop() da atividade, cancele o registro do callback de captura de tela:
O conteúdo e os exemplos de código nesta página estão sujeitos às licenças descritas na Licença de conteúdo. Java e OpenJDK são marcas registradas da Oracle e/ou suas afiliadas.
Última atualização 2025-08-27 UTC.
[null,null,["Última atualização 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```"]]