Intent 是 Android 开发中用于组件间通信的核心机制,掌握其使用方法能显著提升应用交互能力。以下是关键知识点:
一、Intent 核心概念 📌
启动 Activity
Intent intent = new Intent(this, TargetActivity.class); startActivity(intent);
传递数据
- 通过
putExtra()
方法添加数据 - 接收方用
getIntent().getStringExtra()
提取
示例:intent.putExtra("user_name", "张三")
- 通过
隐式 Intent 与显式 Intent
- 隐式:
intent.setAction("android.intent.action.VIEW")
- 显式:指定目标组件类名
- 隐式:
二、常见使用场景 🌐
- 启动系统应用:如调用电话
Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:123456789"));
- 启动服务:
Intent intent = new Intent(this, MyService.class);
- 广播发送:
sendBroadcast(new Intent("custom.intent.action"))
三、进阶技巧 🔍
- 使用
Intent.FLAG_ACTIVITY_NEW_TASK
实现跨应用跳转 - 通过
Intent.createChooser()
弹出应用选择器 - 配合
PendingIntent
实现延迟执行
🔗 想了解更多高级主题?点击这里前往 Android 高级开发指南