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
    )
相关推荐
用户20187928316737 分钟前
通俗易懂理解Java注解
android
用户20187928316737 分钟前
通俗易懂理解泛型
android
linversion38 分钟前
如何手动上传Android混淆映射文件Mapping.txt到Firebase
android
慕晨41 分钟前
RecyclerView + SnapHelper 滚动差异问题
android
玲小珑1 小时前
Auto.js 入门指南(三)第一个 Auto.js 脚本
android·前端
aningxiaoxixi2 小时前
Android Studio 之基础代码解析
android·ide·android studio
A-花开堪折2 小时前
Android7 Input(十)View 处理Input事件pipeline
android·嵌入式硬件
Shujie_L3 小时前
Android基础回顾】六:安卓显示机制Surface 、 SurfaceFlinger、Choreographer
android
海棠一号4 小时前
Android Settings 数据库生成、监听与默认值配置
android·数据库
雨白4 小时前
Fragment 入门教程:从核心概念到实践操作
android