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
    )
相关推荐
AD钙奶-lalala2 小时前
某车企面试备忘
android
我爱拉臭臭3 小时前
kotlin音乐app之自定义点击缩放组件Shrink Layout
android·java·kotlin
匹马夕阳4 小时前
(二十五)安卓开发一个完整的登录页面-支持密码登录和手机验证码登录
android·智能手机
吃饭了呀呀呀4 小时前
🐳 深度解析:Android 下拉选择控件优化方案——NiceSpinner 实践指南
android·java
吃饭了呀呀呀5 小时前
🐳 《Android》 安卓开发教程 - 三级地区联动
android·java·后端
_祝你今天愉快6 小时前
深入剖析Java中ThreadLocal原理
android
张力尹7 小时前
谈谈 kotlin 和 java 中的锁!你是不是在协程中使用 synchronized?
android
流浪汉kylin7 小时前
Android 斜切图片
android
PuddingSama8 小时前
Android 视图转换工具 Matrix
android·前端·面试
RichardLai888 小时前
[Flutter学习之Dart基础] - 控制语句
android·flutter