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
    )
相关推荐
Lei活在当下3 小时前
【Perfetto从入门到精通】2. 使用 Perfetto 追踪/分析 APP 的 Native/Java 内存
android·性能优化·架构
愤怒的代码3 小时前
🔗 深度解析 SystemUI 进程间通信机制(一)
android·操作系统·app
RainyJiang4 小时前
聊聊协程里的 Semaphore:别让协程挤爆门口
android·kotlin
Dev7z6 小时前
在MySQL里创建数据库
android·数据库·mysql
invicinble6 小时前
mysql建立存数据的表(一)
android·数据库·mysql
似霰7 小时前
传统 Hal 开发笔记1----传统 HAL简介
android·hal
Zender Han8 小时前
Flutter Gradients 全面指南:原理、类型与实战使用
android·flutter·ios
火柴就是我8 小时前
Flutter Path.computeMetrics() 的使用注意点
android·flutter
モンキー・D・小菜鸡儿10 小时前
Android 系统TTS(文字转语音)解析
android·tts
2501_9159090610 小时前
iOS 反编译防护工具全景解析 从底层符号到资源层的多维安全体系
android·安全·ios·小程序·uni-app·iphone·webview