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
    )
相关推荐
习惯就好zz8 分钟前
[Android/Linux] 实战记录:利用 Kconfig 精确控制 i.MX8MM 特定 DTB 的编译生成
android·linux·dts·dtb·lunch·多卡板配置
踏雪羽翼40 分钟前
android 解决混淆导致AGPBI: {“kind“:“error“,“text“:“Type a.a is defined multiple times
android·java·开发语言·混淆·混淆打包出现a.a
csj501 小时前
安卓基础之《(21)—高级控件(3)翻页类视图》
android
2501_915918411 小时前
中小团队发布,跨平台 iOS 上架,证书、描述文件创建管理,测试分发一体化方案
android·ios·小程序·https·uni-app·iphone·webview
betazhou1 小时前
MySQL相关性能查询语句
android·数据库·mysql
一起养小猫1 小时前
Flutter for OpenHarmony 进阶:Timer组件与倒计时系统深度解析
android·网络·笔记·flutter·json·harmonyos
符哥20082 小时前
Fastjson2.X 使用详解
android·java
月明泉清2 小时前
Android中对于点击事件的深度梳理(三)
android
电饭叔2 小时前
DataFrame和 Series 索引
android·python
lexiangqicheng2 小时前
【全网最全】React Native 安卓原生工程结构与构建机制深度解析
android·react native·react.js