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

Intent ใน Android มีกี่ประเภท? ทำความเข้าใจ Explicit และ Implicit Intent

Intent ใน Android: 2 ประเภทหลัก

Intent คือ Mechanism สำหรับ Request Action ระหว่าง Components (Activity, Service, BroadcastReceiver)

1. Explicit Intent (ชัดเจน)

ระบุ Component Class โดยตรง ใช้ภายในแอปเดียวกัน

// Start Activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("key", "value"); // ส่งข้อมูล
startActivity(intent);

// Start Service
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
  • ต้อง Declare Target Activity ใน Manifest
  • Compile-time Safety (Class Reference)
  • ใช้สำหรับ: Navigation ภายในแอป, Start Service, Send Broadcast ภายในแอป

2. Implicit Intent (โดยนัย)

ระบุ Action และ Data ให้ System หา Component ที่รองรับ (Intent Filter)

// Open Website
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://tutorialspoint.com"));
startActivity(intent);

// Share Text
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, "Check this out!");
startActivity(Intent.createChooser(share, "Share via"));

// Send Email
Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "test@example.com", null));
email.putExtra(Intent.EXTRA_SUBJECT, "Subject");
email.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(email);

// Dial Phone
Intent dial = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0812345678"));
startActivity(dial);

Intent Filter ใน Manifest (สำหรับรับ Implicit Intent)

<activity android:name=".MyBrowserActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" android:host="tutorialspoint.com" />
    </intent-filter>
</activity>

ตารางเปรียบเทียบ

FeatureExplicit IntentImplicit Intent
TargetSpecific Component ClassAction + Data + Category
ScopeSame App (usually)Cross-app, System
ResolutionCompile-timeRuntime (PackageManager)
SecurityHigh (Private)Lower (Public)
Use CaseInternal NavigationShare, View, Dial, Pick, etc.

ตัวอย่างครบ: Explicit Intent (Start Activity)

// MainActivity.java
Button btn = findViewById(R.id.btnStart);
btn.setOnClickListener(v -> {
    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
    intent.putExtra("message", "Hello from Main!");
    startActivity(intent);
});

// SecondActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    
    String msg = getIntent().getStringExtra("message");
    TextView tv = findViewById(R.id.textView);
    tv.setText(msg);
}

// Manifest
<activity android:name=".SecondActivity" />

ตัวอย่างครบ: Implicit Intent (Open Website)

Button btn = findViewById(R.id.btnWeb);
btn.setOnClickListener(v -> {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("https://tutorialspoint.com"));
    
    // Verify there's an app to handle
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    } else {
        Toast.makeText(this, "No browser found", Toast.LENGTH_SHORT).show();
    }
});

// Manifest: Internet Permission Required
<uses-permission android:name="android.permission.INTERNET" />

Best Practices

  • ✅ ใช้ Explicit Intent สำหรับ Internal Navigation
  • ✅ ใช้ Implicit Intent สำหรับ System Actions (Share, View, Dial, Pick)
  • ✅ ตรวจสอบ resolveActivity() ก่อน startActivity (Implicit)
  • ✅ ใช้ Intent.createChooser() สำหรับ ACTION_SEND (ให้ User เลือกแอป)
  • ✅ เพิ่ม android:exported="true" ใน Manifest สำหรับ Activity ที่รับ Implicit Intent (API 31+)
  • ⚠️ ไม่ควรส่ง Sensitive Data ผ่าน Implicit Intent