© Khmer Angkor Academy - sophearithput168

Intent

Intent អ្វីជា?

Intent គឺជា messaging object ប្រើសម្រាប់ស្នើសុំ action ពី component ផ្សេងទៀត។ វាដូចជា "សារ" ដែលផ្ញើរវាង components នៅក្នុង Android!

💡 Intent Analogy (ប្រៀបធៀប)

គិតអំពី Intent ដូចជា សំបុត្រ ដែលមាន:

  • 📬 អាស័យដ្ឋាន (Destination) - Component ណាដែលត្រូវទទួល
  • 📦 ទិន្នន័យ (Data) - ព័ត៌មានដែលផ្ញើ
  • 🏷️ Action - អ្វីដែលត្រូវធ្វើ (បើក, ចែករំលែក, ផ្ញើ)

🎯 Intent ប្រើសម្រាប់អ្វីខ្លះ?

Purpose ពិពណ៌នា ឧទាហរណ៍
🔄 Start Activity ចាប់ផ្តើម screen ថ្មី ពី Login → Home screen
📞 Start Service ចាប់ផ្តើម background task Download file, Play music
📡 Send Broadcast ផ្ញើសារទៅ apps ច្រើន Battery low notification
📤 Share Data ចែករំលែកទិន្នន័យ ផ្ញើ name, age, email ពី Activity A → B

🔀 Intent ពីរប្រភេទ

Type ពិពណ៌នា ពេលណាប្រើ
1️⃣ Explicit Intent ចង្អុលបង្ហាញ component ច្បាស់លាស់ Navigate រវាង activities ក្នុង app របស់អ្នក
2️⃣ Implicit Intent ស្នើសុំ action ទូទៅ (system ជ្រើសរើស app) បើកតំណ, ហៅទូរស័ព្ទ, ផ្ញើអ៊ីមែល, ចែករំលែក

🔄 Intent Resolution Process

ពេល implicit intent ត្រូវបានផ្ញើ, Android system ជ្រើសរើស app ដោយ:

┌─────────────────────┐
│  Send Intent        │
│  (ACTION_VIEW URL)  │
└──────────┬──────────┘
           ↓
┌──────────▼──────────────────┐
│  System checks Intent       │
│  Filters of all apps        │
└──────────┬──────────────────┘
           ↓
     ┌─────┴─────┐
     ↓           ↓
┌────▼────┐  ┌──▼──────┐
│ Chrome  │  │ Firefox │  ──► Apps that can handle URL
└────┬────┘  └──┬──────┘
     └─────┬─────┘
           ↓
┌──────────▼──────────┐
│  User selects app   │
│  (Chooser Dialog)   │
└──────────┬──────────┘
           ↓
┌──────────▼──────────┐
│  Selected app opens │
└─────────────────────┘

📦 Intent Data Types

Method Data Type ឧទាហរណ៍
putExtra("key", value) String, int, boolean, float, double putExtra("age", 25)
putExtra("key", array) Array (int[], String[]) putExtra("scores", scores)
putExtra("key", object) Serializable, Parcelable putExtra("user", user)
putExtras(bundle) Bundle (multiple values) putExtras(bundle)

🌐 Common Implicit Intents

Action ពិពណ៌នា Code
🌐 បើកតំណ Open URL in browser ACTION_VIEW
📞 ហៅទូរស័ព្ទ Open dialer with number ACTION_DIAL
📧 ផ្ញើអ៊ីមែល Open email app ACTION_SEND
💬 ផ្ញើ SMS Open SMS app ACTION_SENDTO
📷 ថតរូប Open camera ACTION_IMAGE_CAPTURE
📤 ចែករំលែក Share content ACTION_SEND

⚠️ Intent Verification: ត្រូវតែ verify ថាមាន app អាចដោះស្រាយ intent មុនពេលចាប់ផ្តើម! ប្រើ intent.resolveActivity()

Java Code
Click "Run" to execute the Java code