Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีป้องกันการจับภาพหน้าจอ (Screen Recording) ใน Android

ตัวอย่างนี้สาธิตวิธีป้องกันไม่ให้จับภาพหน้าจอ (Screenshot/Screen Recording) ได้ใน Android โดยใช้ Flag FLAG_SECURE บน Window

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/rlMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="หน้านี้ป้องกัน Screenshot/Screen Recording"
        android:textSize="18sp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="50dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ลองกด Power + Volume Down หรือใช้ Screen Recorder"
        android:textSize="14sp"
        android:textColor="#666666"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp" />

</LinearLayout>

ขั้นตอนที่ 3: MainActivity.java

package app.com.sample;

import android.os.Bundle;
import android.view.WindowManager;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 🔑 สำคัญ: ตั้งค่าก่อน setContentView()
        getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE
        );
        
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

คำอธิบาย FLAG_SECURE

  • ป้องกัน Screenshot (Power + Volume Down)
  • ป้องกัน Screen Recording (Built-in หรือ App ภายนอก)
  • ป้องกัน Remote Display (Cast/Mirror)
  • หน้าจอจะแสดงสีดำ/ว่างเปล่า ในการจับภาพ/บันทึกวิดีโอ

วิธีใช้กับ Activity เฉพาะบางหน้า

// เปิดใช้งาน
getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_SECURE,
    WindowManager.LayoutParams.FLAG_SECURE
);

// ปิดใช้งาน (ถ้าต้องการให้หน้าถัดไปจับภาพได้)
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SECURE);

ใช้ใน Dialog/BottomSheet

// สำหรับ Dialog
Dialog dialog = new Dialog(this);
dialog.getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_SECURE,
    WindowManager.LayoutParams.FLAG_SECURE
);
dialog.setContentView(R.layout.dialog_layout);

// สำหรับ BottomSheetDialog
BottomSheetDialog dialog = new BottomSheetDialog(this);
dialog.getWindow().setFlags(...);

ข้อจำกัดและข้อควรระวัง

  • ❌ ไม่ป้องกันการถ่ายรูปหน้าจอด้วยกล้องภายนอก (Physical Camera)
  • ❌ บน Emulator บางตัวอาจไม่ทำงาน
  • ❌ ไม่ป้องกันการ Accessibility Service อ่านหน้าจอ (TalkBack, Screen Readers)
  • ⚠️ ใช้เฉพาะหน้าที่มีข้อมูลละเอียดอ่อน (Login, Payment, Personal Data)
  • ⚠️ อย่าใช้กับทุก Activity - จะทำให้ผู้ใช้ไม่สแปร์หน้าจอได้เลย

การทดสอบ

  1. Install แอปบนอุปกรณ์จริง
  2. เปิดแอป Screen Recorder (เช่น AZ Screen Recorder, มาตรฐาน Android 11+)
  3. เริ่มบันทึกหน้าจอ
  4. เปิดแอปนี้ → หน้าจอจะแสดงสีดำ/ว่างในวิดีโอที่บันทึก
  5. ลอง Screenshot (Power + Vol Down) → จะขึ้นข้อความ "Couldn't capture screenshot"

ผลลัพธ์ที่คาดหวัง

  • ก่อน Screen Recording: เห็นเนื้อหาปกติ
  • ระหว่าง/หลัง Screen Recording: วิดีโอที่ได้จะเป็นหน้าจอดำ/ว่างเปล่า