You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter应用中检测录屏并触发退出功能的实现需求

Flutter实现录屏检测并自动关闭应用

针对Android和iOS平台,分别通过原生代码实现录屏检测逻辑,一旦检测到录屏行为立即关闭应用:

Android 实现

在MainActivity.kt中添加录屏检测逻辑,通过AppOpsManager检查屏幕录制权限的使用状态,定时轮询检测:

import android.app.AppOpsManager
import android.content.Context
import android.os.Bundle
import android.os.Process
import io.flutter.embedding.android.FlutterActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : FlutterActivity() {
    private val appOpsManager by lazy { getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        startScreenRecordingDetection()
    }
    
    private fun startScreenRecordingDetection() {
        CoroutineScope(Dispatchers.Default).launch {
            while (true) {
                delay(1000) // 每秒执行一次检测
                if (isScreenRecording()) {
                    runOnUiThread {
                        finishAffinity() // 关闭所有Activity
                        System.exit(0)
                    }
                    break
                }
            }
        }
    }
    
    private fun isScreenRecording(): Boolean {
        val mode = appOpsManager.checkOpNoThrow(
            AppOpsManager.OPSTR_PROJECT_MEDIA,
            Process.myUid(),
            packageName
        )
        return mode == AppOpsManager.MODE_ALLOWED
    }
}

iOS 实现

在AppDelegate.swift中监听系统的屏幕录制状态变化通知,一旦检测到录屏立即退出应用:

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        
        // 注册屏幕录制状态监听
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(screenCaptureStateChanged),
            name: UIScreen.capturedDidChangeNotification,
            object: nil
        )
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    @objc private func screenCaptureStateChanged(_ notification: Notification) {
        guard let screen = notification.object as? UIScreen, screen.isCaptured else {
            return
        }
        // 检测到录屏,直接退出应用
        exit(0)
    }
}

说明

  • 原生层启动后会自动开始监听/轮询,无需Flutter层额外调用
  • 你之前遇到的录屏黑屏问题,是因为可能设置了FLAG_SECURE(Android)或系统隐私保护机制,该逻辑可以保留,同时配合上述检测关闭逻辑使用
  • Android方案适配API 21+,iOS方案适配iOS 11+,覆盖绝大多数主流设备

内容的提问来源于stack exchange,提问作者Nassim Solimam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 23:11:17