Android 中Intent 相关问题

在回答 Intent 问题时,清晰区分其 定义类型应用场景。以下是的回答策略:


一、Intent 的核心定义

Intent 是 Android 系统中的 消息传递对象,主要用于三大场景:

2. 隐式 Intent(Implicit Intent)

三、Intent 的关键组成要素

属性 作用 示例值
Component 显式指定目标组件 ComponentName(pkg, cls)
Action 描述操作类型(常量) Intent.ACTION_VIEW
Category 补充Action的上下文信息 Intent.CATEGORY_BROWSABLE
Data URI格式的数据(通常与Type配合) Uri.parse("http://example.com")
Type MIME类型 "image/png"
Extras 附加数据的Bundle putExtra("id", 123)
Flags 控制启动模式 FLAG_ACTIVITY_CLEAR_TOP

四、高级使用场景

1. PendingIntent
2. 深度链接(Deep Link)
3. Intent 过滤器冲突解决

当多个 Activity 声明相同 <intent-filter> 时:


五、面试高频问

  1. 组件间通信:启动 Activity/Service,发送广播

  2. 数据传递:携带附加数据(Bundle)

  3. 隐式调用:声明式匹配系统或第三方组件

    二、Intent 的两种核心类型

1. 显式 Intent(Explicit Intent)

  1. 特点:明确指定目标组件(类名)

  2. 使用场景:应用内部组件跳转

  3. 优势:高效直接,无需系统解析

  4. 示例

    kotlin

    复制

    复制代码
    // 启动Service
    Intent(this, MyService::class.java).also { 
        startService(it)
    }
  5. 特点:通过 Action、Category、Data 等属性匹配组件

  6. 使用场景:跨应用调用(如分享、打开链接)

  7. 匹配规则

    xml

    复制

    复制代码
    <!-- AndroidManifest.xml 声明 -->
    <activity android:name=".ShareActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="text/plain" />
        </intent-filter>
    </activity>

    运行 HTML

  8. 示例

    kotlin

    复制

    复制代码
    // 调用系统分享
    Intent(Intent.ACTION_SEND).apply {
        type = "text/plain"
        putExtra(Intent.EXTRA_TEXT, "分享内容")
        startActivity(Intent.createChooser(this, "选择分享应用"))
    }
  9. 特点:允许外部应用以当前应用身份执行 Intent

  10. 应用场景:通知栏点击、AlarmManager

  11. 配置

    xml

    复制

    复制代码
    <intent-filter>
        <data android:scheme="https" 
              android:host="example.com" 
              android:pathPrefix="/detail"/>
        <!-- 必须添加 -->
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

    运行 HTML

  12. 验证

    bash

    复制

    复制代码
    adb shell am start -W -a android.intent.action.VIEW -d "https://example.com/detail?id=123"
  13. 系统弹出选择器(ResolverActivity)

  14. 可通过 PackageManager.queryIntentActivities() 获取匹配列表

  15. 优先选择优先级高的组件(<intent-filter android:priority="100">

  16. 示例

    kotlin

    复制

    复制代码
    val pendingIntent = PendingIntent.getActivity(
        this, 0, intent, 
        PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )
相关推荐
Jomurphys3 小时前
Compose 封装 - 点击防抖
android
乾坤一气杀5 小时前
OkHttp3 内部工作原理时序图
android
一起搞IT吧6 小时前
相机拍照无响应问题分析一:【MEMORY_NOT_ENOUGH导致】持续快拍,一会儿无法拍照了
android·c++·数码相机·智能手机
是店小二呀7 小时前
【MySQL】MySQL 从安装到理解
android·mysql·adb
we1less8 小时前
[audio] threadLoop_write 到 audio-hal 分析
android
冬奇Lab10 小时前
一次必现ANR问题的深度分析与解决之旅:当NestedScrollView遇上VelocityTracker
android·性能优化·debug
三少爷的鞋10 小时前
2025 技术总结:我把技术重新结构化的一年
android
叶羽西10 小时前
查Google Android某个子仓库的修改情况
android
a1760293175711 小时前
3DS模拟器 Azahar模拟器最新版 安卓汉化中文版+PC版附3DS中文游戏资源全集+3DS密匙key和字库
android·游戏
山山而川 潺潺如镜12 小时前
python防止程序多开,但程序运行脚本
android·开发语言·python