บทนำ
Intent Filter เป็นอินสแตนซ์ของคลาส IntentFilter ใช้ระบุว่า Component (Activity, Service, BroadcastReceiver) ตอบสนองต่อ Intent ประเภทไหนได้ ช่วยให้ Android รู้ว่าจะเปิด Component ไหนเมื่อมี Implicit Intent
ตัวอย่างนี้สาธิตการลงทะเบียน Dynamic Broadcast Receiver พร้อม Custom Intent Filter ผ่านโค้ด Java (ไม่ใช่ใน Manifest)
ขั้นตอนที่ 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"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/buton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Intent Filter"
android:layout_marginBottom="16dp" />
<Button
android:id="@+id/buton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Data" />
</LinearLayout>
ปุ่ม 2 ปุ่ม: Register (ลงทะเบียน Receiver) และ Send Data (ส่ง Broadcast)
ขั้นตอนที่ 3: เขียนโค้ด Java (src/MainActivity.java)
package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
// Custom Action สำหรับ Broadcast
private static final String CUSTOM_ACTION = "com.example.andy.CUSTOM_INTENT";
// Broadcast Receiver แบบ Dynamic (ประกาศเป็น Field เพื่อ Unregister ได้)
private final BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// ดึง Action จาก Intent
String action = intent.getAction();
Toast.makeText(MainActivity.this, "Received: " + action, Toast.LENGTH_LONG).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnRegister = findViewById(R.id.buton);
final Button btnSend = findViewById(R.id.buton1);
// ปุ่ม Send Broadcast
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CUSTOM_ACTION);
sendBroadcast(intent);
}
});
// ปุ่ม Register Receiver
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IntentFilter filter = new IntentFilter(CUSTOM_ACTION);
registerReceiver(mMessageReceiver, filter);
Toast.makeText(MainActivity.this, "Receiver Registered", Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onPause() {
super.onPause();
// สำคัญ: Unregister Receiver เมื่อ Activity หยุดทำงาน เพื่อป้องกัน Memory Leak
try {
unregisterReceiver(mMessageReceiver);
} catch (IllegalArgumentException e) {
// Receiver ยังไม่ได้ Register - ไม่เป็นไร
}
}
}
คำอธิบายสำคัญ
1. Dynamic vs Static Registration
| ลักษณะ | Dynamic (โค้ด Java) | Static (Manifest) |
|---|---|---|
| ต้อง Unregister | ต้อง (ใน onPause/onDestroy) | ไม่ต้อง |
| ทำงานได้เมื่อแอปปิด | ไม่ได้ | ได้ (ถ้า exported=true) |
| ยืดหยุ่น | สูง (Register/Unregister ได้ตลอดเวลา) | คงที่ |
2. Intent Filter Testing
Android ทดสอบ Intent กับ Filter 3 ขั้นตอน:
- Action Test - Action ใน Intent ตรงกับที่ Filter ประกาศไหม
- Category Test - Category ใน Intent ตรงทั้งหมดกับ Filter ไหม
- Data Test - Data/Type ใน Intent ตรงกับ Filter ไหม
การทดสอบแอปพลิเคชัน
รันแอปบนอุปกรณ์จริง

กด Register Intent Filter แล้วกด Send Data - จะแสดง Toast รับ Action

สรุป
ใช้ registerReceiver(receiver, filter) พร้อม IntentFilter เพื่อรับ Custom Broadcast แบบ Dynamic ต้อง Unregister ใน onPause() เสมอ