# flutter_launcher_app **Repository Path**: zsh_dev/flutter_launcher_app ## Basic Information - **Project Name**: flutter_launcher_app - **Description**: 通过应用包名、url 方式唤起app(支持安卓、iOS、鸿蒙) - **Primary Language**: Dart - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-22 - **Last Updated**: 2026-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: plugin ## README # launcher_app 一个用于「通过包名 / URL Scheme 唤起其他应用」的 Flutter 插件,支持 **Android / iOS / HarmonyOS NEXT** 三端。 唤起前会先检查目标 App 是否安装;未安装时返回 `false`,调用方自行提示。 --- ## 一、快速开始 ### 1. 添加依赖 ```yaml # pubspec.yaml dependencies: launcher_app: path: ../launcher_app # 或发布到 pub 后用版本号 ``` ### 2. 调用 ```dart import 'package:launcher_app/launcher_app.dart'; final launcher = LauncherApp(); const wechat = LaunchConfig( androidPackage: 'com.tencent.mm', iosUrlScheme: 'weixin://', harmonyBundle: 'com.tencent.wechat', harmonyUrl: 'weixin://', ); if (await launcher.isInstalled(wechat)) { await launcher.launch(wechat); } else { // 提示「未安装微信」 } ``` --- ## 二、API ### `LaunchConfig` | 字段 | 平台 | 说明 | | ---------------- | --------- | -------------------------------------- | | `androidPackage` | Android | 目标应用包名,如 `com.tencent.mm` | | `iosUrlScheme` | iOS | 目标应用注册的 URL Scheme,如 `weixin://` | | `harmonyBundle` | HarmonyOS | 目标应用 bundleName,如 `com.tencent.wechat` | | `harmonyModule` | HarmonyOS | 模块名(可选,配合 `harmonyAbility` 精确启动) | | `harmonyAbility` | HarmonyOS | Ability 名(可选) | | `harmonyUrl` | HarmonyOS | URL Scheme,如 `weixin://` | ### `LauncherApp` | 方法 | 说明 | | ------------------------------- | -------------------- | | `Future isInstalled(cfg)` | 检查目标应用是否安装 | | `Future launch(cfg)` | 唤起目标应用,未安装返回 `false` | --- ## 三、平台实现原理 | 平台 | 检查安装 | 唤起 | | --------- | ------------------------------------------ | --------------------------------------------------------------------------------- | | Android | `PackageManager.getLaunchIntentForPackage` | 同上的 Intent + `startActivity` (FLAG_ACTIVITY_NEW_TASK) | | iOS | `UIApplication.canOpenURL` | `UIApplication.open(url:)` | | HarmonyOS | `bundleManager.canOpenLink` (URL 路径) | `openLink` (URL) 或 `startAbility({bundleName, moduleName, abilityName})` (Bundle) | > ⚠️ **iOS 无法通过 Bundle ID 唤起其他 App**(沙盒限制),只能用 URL Scheme。 > ⚠️ **HarmonyOS NEXT 普通应用无法查询其他 bundle 是否安装**(需要 `GET_BUNDLE_INFO_PRIVILEGED` 系统权限)。Bundle 路径下 `isInstalled` 始终返回 `true`,靠 `launch` 失败来推断未安装。 --- ## 四、宿主 App 必须的配置 > 这部分**插件管不到,必须由接入方在自己工程里声明**。 ### Android (`android/app/src/main/AndroidManifest.xml`) Android 11 (API 30) 起,必须在 `` 中声明要查询/唤起的包名,否则 `getLaunchIntentForPackage` 永远返回 null。 ```xml ``` ### iOS (`ios/Runner/Info.plist`) iOS 9+ 必须在 `LSApplicationQueriesSchemes` 中声明要查询的 scheme,否则 `canOpenURL` 永远返回 `false` 并在控制台报警告。 ```xml LSApplicationQueriesSchemes weixin alipay alipays ``` ### HarmonyOS (`ohos/entry/src/main/module.json5`) 如果用 URL Scheme 路径(`harmonyUrl`),需要在 `querySchemes` 中声明: ```json5 { "module": { // ... "querySchemes": [ "weixin", "alipays" ] } } ``` 如果只用 Bundle 路径(`harmonyBundle` + `harmonyModule` + `harmonyAbility`),不需要配置 `querySchemes`。 --- ## 五、各平台特殊说明 ### Android - 国产 ROM(MIUI / EMUI / HarmonyOS 兼容版)可能需要用户授权"应用关联启动"权限,否则 `startActivity` 会被静默拦截 - 跨进程启动 Activity 必须加 `FLAG_ACTIVITY_NEW_TASK`,插件内已处理 - 一个目标包名通过 `getLaunchIntentForPackage` 返回 null 但实际安装了,多半是 `` 没声明 ### iOS - **不存在「通过 Bundle ID 唤起」**,请勿期望 - `LSApplicationQueriesSchemes` 白名单**上限 50 个 scheme** - Universal Links (HTTPS) 也能唤起 App,但**无法判断是否安装** —— 未安装时退化到 Safari 打开网页 - iOS 14+ 在主线程外调用 `open(url:)` 可能崩,插件内已确保在主线程 ### HarmonyOS NEXT - **包名和 Android 不一样**:例如微信的 Android 包名是 `com.tencent.mm`,鸿蒙版是 `com.tencent.wechat`。可在设备上用 `hdc shell bm dump -a` 查实际 bundleName - **bundle 路径 + action.system.home 不一定能唤起**:如果目标 Ability 的 skill 声明了 `uris`(host/path 等),靠 `action + entity` 匹配不上。此时必须用 `harmonyModule + harmonyAbility` 显式指定 - 想精确启动某个 Ability 时,可用 `hdc shell bm dump -n ` 查 ability 列表 - 普通第三方应用**无法**用 `bundleManager.getBundleInfo` 查任意 bundle(需要 `GET_BUNDLE_INFO_PRIVILEGED` 系统权限)。所以 bundle 路径下 `isInstalled` 是"乐观返回",靠 `launch` 失败判断 --- ## 六、错误码 `launch` 返回 `false` 时,鸿蒙端会捕获以下错误码并视作"未安装": | 错误码 | 含义 | | ---------- | -------------------------- | | `16000001` | bundle not found(未安装) | | `16000019` | no matching ability(能力不存在) | 其他错误会抛 `PlatformException(code: 'LAUNCH_FAILED', message: 'code=xxx ...')`,前端可以 try/catch 拿到详细错误信息。 --- ## 七、调试技巧 ### Android ```bash adb shell pm list packages | grep <关键词> # 查包名 adb shell dumpsys package | grep -A2 Activity # 查 launcher activity ``` ### iOS - Xcode 控制台搜 `LSApplicationQueriesSchemes` 看是否有白名单警告 - 真机:设置 → 隐私 → 允许跳转 ### HarmonyOS ```bash hdc shell bm dump -a # 列出所有 bundle hdc shell bm dump -n # 查 bundle 详细信息(skills / abilities / scheme) hdc shell hilog -r # 清空日志 hdc shell hilog -x | grep LauncherAppPlugin # 看插件日志 ```