使用Flutter WindowManager禁用截图后,如何检测截图触发事件?
检测截图尝试并提示用户
你已经通过FlutterWindowManager设置FLAG_SECURE禁用了截图,但该标记仅能阻止系统保存有效截图内容,无法直接捕获用户的截图操作。要实现检测截图尝试并提示用户,需要针对Android和iOS分别做原生层的监听,再通过MethodChannel通知Flutter端触发提示:
Android 端实现
Android系统在用户触发截图时会发送android.intent.action.SCREENSHOT广播,我们可以通过广播接收器监听该事件:
- 编写Android原生广播接收器代码:
// MainActivity.kt class ScreenshotReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { if (intent?.action == "android.intent.action.SCREENSHOT") { MethodChannel(flutterEngine?.dartExecutor?.binaryMessenger, "screenshot_channel") .invokeMethod("onScreenshotAttempt", null) } } }
- 在MainActivity中注册/注销广播接收器:
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val filter = IntentFilter("android.intent.action.SCREENSHOT") registerReceiver(ScreenshotReceiver(), filter) } override fun onDestroy() { super.onDestroy() unregisterReceiver(ScreenshotReceiver()) }
- Flutter端监听回调并显示提示:
import 'package:flutter/services.dart'; import 'package:flutter/material.dart'; import 'package:flutter_windowmanager/flutter_windowmanager.dart'; class SecureScreen extends StatefulWidget { @override _SecureScreenState createState() => _SecureScreenState(); } class _SecureScreenState extends State<SecureScreen> { static const MethodChannel _channel = MethodChannel('screenshot_channel'); bool secureMode = false; @override void initState() { super.initState(); _initSecureMode(); _listenScreenshotAttempt(); } Future<void> _initSecureMode() async { await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE); setState(() { secureMode = true; }); } void _listenScreenshotAttempt() { _channel.setMethodCallHandler((call) async { if (call.method == 'onScreenshotAttempt') { // 用Snackbar提示用户 ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('本应用已禁用截图功能'), duration: Duration(seconds: 2), ), ); // 若要使用Toast,可引入fluttertoast包后替换为: // Fluttertoast.showToast(msg: '本应用已禁用截图功能'); } }); } @override Widget build(BuildContext context) { return Scaffold( // 你的页面布局内容 ); } }
iOS 端实现
iOS没有截图动作的直接回调,但可以监听系统的UIApplicationUserDidTakeScreenshotNotification通知:
- 编写iOS原生监听代码:
// AppDelegate.swift override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { NotificationCenter.default.addObserver( self, selector: #selector(onScreenshotTaken), name: UIApplication.userDidTakeScreenshotNotification, object: nil ) return super.application(application, didFinishLaunchingWithOptions: launchOptions) } @objc func onScreenshotTaken() { let channel = FlutterMethodChannel(name: "screenshot_channel", binaryMessenger: window?.rootViewController as! FlutterBinaryMessenger) channel.invokeMethod("onScreenshotAttempt", arguments: nil) }
- Flutter端的监听和提示逻辑与Android端完全一致,直接复用上述代码即可。
注意事项
- Android的
SCREENSHOT广播并非所有厂商都支持,部分定制ROM可能不会发送该广播,需做兼容性测试。 - iOS的通知会在截图完成后触发,此时截图已生成但内容为空,提示可告知用户截图无效。
内容的提问来源于stack exchange,提问作者Scodes
相关产品推荐
相关产品推荐

