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.}};
在 activity 的 onStart() 方法中,注册屏幕截图回调。
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);}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Detect when users take device screenshots\n\n**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-------------------\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--------------------\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--------------------------------------\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\n### Kotlin\n\n```kotlin\nactivity.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE)\n```\n\n### Java\n\n```java\nactivity.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);\n```"]]