Intent trong Android là gì?
Intent là đối tượng mô tả một hành động sẽ được thực hiện. Dùng để: khởi động Activity, gửi Broadcast, bắt đầu Service, truyền dữ liệu giữa các thành phần (component).
Hai loại Intent
| Loại | Mô tả | Ví dụ |
|---|---|---|
| Explicit Intent | Chỉ định rõ component đích (class). Dùng trong app. | Mở Activity B từ Activity A |
| Implicit Intent | Khai báo action (VIEW, SEND, DIAL...), hệ thống tìm app xử lý. | Mở trang web, gọi điện, chia sẻ ảnh |
Ví dụ Explicit Intent: Chuyển Activity
1. Layout Activity A (activity_main.xml)
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gửi sang Activity khác" />
2. Layout Activity B (activity_second.xml)
<TextView
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
3. MainActivity.java - Khởi động Activity B
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = findViewById(R.id.send);
send.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
});
}
}
4. SecondActivity.java
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView data = findViewById(R.id.data);
data.setText("Đây là Activity thứ hai");
}
}
5. AndroidManifest.xml - Khai báo Activity
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" />
6. Kết quả

Truyền dữ liệu kèm theo Intent
// Gửi
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key_name", "Giá trị");
intent.putExtra("key_age", 25);
startActivity(intent);
// Nhận
String name = getIntent().getStringExtra("key_name");
int age = getIntent().getIntExtra("key_age", 0);
Ví dụ Implicit Intent: Mở trang web
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.tutorialspoint.com"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
Kết luận
Intent là "cây cầu" giữa các component Android. Hiểu rõ Explicit vs Implicit là nền tảng lập trình Android.