Screen recording callback

      Technique summary
    Technique Screen recording callback
    Against Screen recording attacks
    Limitations Android ≥15
    Permission DETECT_SCREEN_RECORDING is required
    Side effects None
    Recommendations Recommended for use for applications running on Android 15 and newer

    Android 15 provides a means to know when there is a change on the screen recording state. It is possible to register a callback to be called whenever there is a change. The callback receives the state, which can be compared with the values WindowManager.SCREEN_RECORDING_STATE_VISIBLE and WindowManager.SCREEN_RECORDING_STATE_NOT_VISIBLE. The callback can run on the main thread or in a background thread.

    For example:

    public void enableScreenDetection(Activity activity) { Context context = activity.getApplicationContext(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { Log.d(TAG, "Enabling screen detection (only for Android 15)"); Executor executor = Executors.newSingleThreadExecutor(); // background thread // Executor executor = context.getMainExecutor(); // main thread windowManager.addScreenRecordingCallback(executor, state -> { if (state == WindowManager.SCREEN_RECORDING_STATE_VISIBLE) { showMessageOnToast(activity, "[!] Screen recording started!"); Log.d(TAG, "Recording"); } else if (state == WindowManager.SCREEN_RECORDING_STATE_NOT_VISIBLE) { showMessageOnToast(activity, "[!] Screen recording stopped"); Log.d(TAG, "Not recording"); } }); } else { showMessageOnToast(activity, "Screen detection is only available from Android 15"); } } private void showMessageOnToast(Activity activity, String message) { activity.runOnUiThread(() -> Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()); }

    Guardsquare

    Table of contents