Menus

Thursday, 3 July 2025

Step-by-Step: Android Secure Screen Without Plugin - Restrict screen short and screen recording

 1. Modify Android Code (MainActivity.java or MainActivity.kt)

If using Kotlin (MainActivity.kt):

import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
import android.view.WindowManager

class MainActivity: FlutterActivity() {
    private val CHANNEL = "secure_screen_channel"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            if (call.method == "enableSecureScreen") {
                window.setFlags(
                    WindowManager.LayoutParams.FLAG_SECURE,
                    WindowManager.LayoutParams.FLAG_SECURE
                )
                result.success(null)
            } else if (call.method == "disableSecureScreen") {
                window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
                result.success(null)
            } else {
                result.notImplemented()
            }
        }
    }
}



2. Flutter Side (Dart)

Create a helper class or call directly:

import 'package:flutter/services.dart';


class SecureScreen {

  static const _channel = MethodChannel('secure_screen_channel');


  static Future<void> enable() async {

    await _channel.invokeMethod('enableSecureScreen');

  }


  static Future<void> disable() async {

    await _channel.invokeMethod('disableSecureScreen');

  }

}


Then in your widget or screen:

@override
void initState() {
  super.initState();
  SecureScreen.enable();
}

@override
void dispose() {
  SecureScreen.disable();
  super.dispose();
}



No comments:

Post a Comment