Intent与IntentFilter #
一、Intent概述 #
Intent是Android中用于组件间通信的消息对象,可以用于启动Activity、Service,发送广播等。Intent封装了组件通信所需的信息,是Android组件间通信的核心机制。
1.1 Intent的用途 #
| 用途 | 方法 |
|---|---|
| 启动Activity | startActivity()、startActivityForResult() |
| 启动Service | startService()、bindService() |
| 发送广播 | sendBroadcast()、sendOrderedBroadcast() |
1.2 Intent的类型 #
| 类型 | 说明 |
|---|---|
| 显式Intent | 明确指定目标组件(类名) |
| 隐式Intent | 不指定目标组件,由系统匹配 |
二、显式Intent #
2.1 基本用法 #
kotlin
// 方式1:通过类名
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
// 方式2:通过ComponentName
val intent = Intent()
intent.component = ComponentName(this, "com.example.myapp.SecondActivity")
startActivity(intent)
// 方式3:通过包名和类名
val intent = Intent()
intent.setClassName("com.example.myapp", "com.example.myapp.SecondActivity")
startActivity(intent)
2.2 传递数据 #
kotlin
// 基本数据类型
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra("name", "Kotlin")
putExtra("age", 10)
putExtra("score", 95.5)
putExtra("isPass", true)
}
startActivity(intent)
// 接收数据
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val name = intent.getStringExtra("name")
val age = intent.getIntExtra("age", 0)
val score = intent.getDoubleExtra("score", 0.0)
val isPass = intent.getBooleanExtra("isPass", false)
}
}
2.3 传递Bundle #
kotlin
val bundle = Bundle().apply {
putString("name", "Kotlin")
putInt("age", 10)
}
val intent = Intent(this, SecondActivity::class.java).apply {
putExtras(bundle)
}
startActivity(intent)
// 接收
val bundle = intent.extras
val name = bundle?.getString("name")
2.4 传递对象 #
kotlin
// 实现Parcelable
@Parcelize
data class User(val id: Int, val name: String) : Parcelable
// 传递
val user = User(1, "Kotlin")
val intent = Intent(this, SecondActivity::class.java).apply {
putExtra("user", user)
}
startActivity(intent)
// 接收
val user = intent.getParcelableExtra<User>("user")
2.5 传递集合 #
kotlin
// 传递List
val list = arrayListOf("A", "B", "C")
val intent = Intent(this, SecondActivity::class.java).apply {
putStringArrayListExtra("list", list)
}
startActivity(intent)
// 接收
val list = intent.getStringArrayListExtra("list")
三、隐式Intent #
3.1 基本用法 #
隐式Intent不指定具体组件,而是通过Action、Category、Data等信息让系统匹配合适的组件。
kotlin
// 打开网页
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.example.com")
startActivity(intent)
// 拨打电话
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:10086")
startActivity(intent)
// 发送短信
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("smsto:10086")
intent.putExtra("sms_body", "Hello")
startActivity(intent)
// 发送邮件
val intent = Intent(Intent.ACTION_SEND)
intent.type = "message/rfc822"
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("test@example.com"))
intent.putExtra(Intent.EXTRA_SUBJECT, "主题")
intent.putExtra(Intent.EXTRA_TEXT, "内容")
startActivity(intent)
3.2 常见系统Action #
| Action | 说明 |
|---|---|
| Intent.ACTION_VIEW | 查看数据 |
| Intent.ACTION_DIAL | 拨号界面 |
| Intent.ACTION_CALL | 直接拨打 |
| Intent.ACTION_SEND | 发送数据 |
| Intent.ACTION_SENDTO | 发送消息 |
| Intent.ACTION_PICK | 选择数据 |
| Intent.ACTION_CAMERA_BUTTON | 相机按钮 |
| Intent.ACTION_WEB_SEARCH | Web搜索 |
3.3 IntentFilter配置 #
在AndroidManifest.xml中配置IntentFilter:
xml
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="com.example.MY_ACTION" />
<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" />
<data android:scheme="http" />
<data android:host="www.example.com" />
</intent-filter>
</activity>
3.4 IntentFilter匹配规则 #
Action匹配 #
Intent中的Action必须与IntentFilter中的某个Action匹配:
xml
<intent-filter>
<action android:name="com.example.ACTION_ONE" />
<action android:name="com.example.ACTION_TWO" />
</intent-filter>
kotlin
// 匹配成功
val intent = Intent("com.example.ACTION_ONE")
// 匹配成功
val intent = Intent("com.example.ACTION_TWO")
// 匹配失败
val intent = Intent("com.example.ACTION_THREE")
Category匹配 #
Intent中的所有Category必须在IntentFilter中存在:
xml
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="com.example.MY_CATEGORY" />
</intent-filter>
kotlin
// 匹配成功
val intent = Intent("com.example.ACTION")
intent.addCategory(Intent.CATEGORY_DEFAULT)
// 匹配成功
val intent = Intent("com.example.ACTION")
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.addCategory("com.example.MY_CATEGORY")
// 匹配失败
val intent = Intent("com.example.ACTION")
intent.addCategory("com.example.UNKNOWN_CATEGORY")
Data匹配 #
Data由URI和MIME类型组成:
text
URI结构:scheme://host:port/path
示例:
https://www.example.com:8080/path
content://com.example.provider/users
tel:10086
xml
<intent-filter>
<data android:scheme="https" android:host="www.example.com" />
<data android:scheme="http" />
<data android:mimeType="image/*" />
</intent-filter>
kotlin
// 匹配成功
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.example.com")
// 匹配成功
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("http://any.host.com")
// 匹配成功
val intent = Intent(Intent.ACTION_VIEW)
intent.type = "image/png"
四、Intent常用属性 #
4.1 Component #
明确指定目标组件:
kotlin
val intent = Intent()
intent.component = ComponentName("com.example.myapp", "com.example.myapp.SecondActivity")
startActivity(intent)
4.2 Action #
指定要执行的操作:
kotlin
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.example.com")
startActivity(intent)
4.3 Data #
指定操作的数据:
kotlin
// URI
intent.data = Uri.parse("content://com.example.provider/users")
// MIME类型
intent.type = "image/*"
// 同时设置
intent.setDataAndType(Uri.parse("file:///sdcard/image.jpg"), "image/*")
4.4 Category #
指定组件的附加信息:
kotlin
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.addCategory(Intent.CATEGORY_DEFAULT)
intent.addCategory(Intent.CATEGORY_BROWSABLE)
4.5 Extras #
携带额外数据:
kotlin
intent.putExtra("name", "Kotlin")
intent.putExtra("age", 10)
// 或使用Bundle
val bundle = Bundle()
bundle.putString("name", "Kotlin")
intent.putExtras(bundle)
4.6 Flags #
指定启动模式和行为:
kotlin
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
五、常用Flags #
| Flag | 说明 |
|---|---|
| FLAG_ACTIVITY_NEW_TASK | 在新任务中启动Activity |
| FLAG_ACTIVITY_CLEAR_TASK | 启动前清除任务栈 |
| FLAG_ACTIVITY_CLEAR_TOP | 清除目标Activity之上的所有Activity |
| FLAG_ACTIVITY_SINGLE_TOP | 相当于singleTop启动模式 |
| FLAG_ACTIVITY_NO_HISTORY | 不保留在任务栈中 |
| FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | 不出现在最近任务列表 |
| FLAG_RECEIVER_REGISTERED_ONLY | 只发送给动态注册的接收者 |
六、Intent最佳实践 #
6.1 使用Intent.createChooser #
kotlin
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "分享内容")
val chooser = Intent.createChooser(intent, "选择分享方式")
startActivity(chooser)
6.2 检查Intent是否可处理 #
kotlin
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://www.example.com")
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
} else {
Toast.makeText(this, "没有应用可以处理此请求", Toast.LENGTH_SHORT).show()
}
6.3 安全传递敏感数据 #
kotlin
// 不要在Intent中传递敏感数据
// 错误:
intent.putExtra("password", "123456")
// 正确:使用临时权限或加密
6.4 使用PendingIntent #
PendingIntent是对Intent的包装,可以在未来执行:
kotlin
// 通知中点击跳转
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("标题")
.setContentText("内容")
.setContentIntent(pendingIntent)
.build()
七、总结 #
本章详细介绍了Intent与IntentFilter:
- Intent的基本概念和类型
- 显式Intent的使用方法
- 隐式Intent的使用方法
- IntentFilter的匹配规则
- Intent的常用属性
- 常用Flags
- 最佳实践
Intent是Android组件间通信的核心机制,掌握Intent的使用对于Android开发至关重要。
最后更新:2026-03-26