You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Flutter/Dart中实现从应用启动手机其他应用?附原生Android实现示例

在Flutter/Dart中启动其他应用的实现方案

嘿,刚好做过类似的需求!在Flutter里实现唤起其他应用的逻辑,思路和你原生Android用Intent的方式本质是一样的——都是通过URL Scheme来触发,但Flutter本身没有内置这个能力,得借助官方推荐的url_launcher插件来完成,我给你一步步拆解:

1. 引入依赖包

首先在你的pubspec.yaml文件中添加url_launcher的依赖,记得用pub上的最新版本(可以去pub.dev查一下当前最新版):

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.2.5 # 替换为最新版本

添加完后运行flutter pub get完成依赖安装。

2. 导入包到代码中

在需要实现唤起逻辑的Dart文件顶部导入:

import 'package:url_launcher/url_launcher.dart';

3. 核心唤起逻辑实现

我们通过URL Scheme来定位要唤起的应用,写一个异步方法来处理唤起逻辑,同时处理应用未安装或不支持的情况:

Future<void> launchTargetApp() async {
  // 示例:唤起微信,微信的URL Scheme是weixin://
  const targetScheme = 'weixin://';
  final uri = Uri.parse(targetScheme);

  // 先判断设备是否能打开这个Scheme
  if (await canLaunchUrl(uri)) {
    // 执行唤起操作
    await launchUrl(uri);
  } else {
    // 唤起失败时的提示,你可以换成弹窗或者其他提示方式
    throw '无法唤起目标应用,请检查是否已安装';
  }
}

对应你原生的URL跳转场景

如果你要实现类似原生startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)))打开浏览器的逻辑,也可以用这个插件,只需要指定启动模式为外部应用:

Future<void> launchExternalBrowser(String url) async {
  final uri = Uri.parse(url);
  if (await canLaunchUrl(uri)) {
    // 指定用外部浏览器打开,而不是应用内WebView
    await launchUrl(uri, mode: LaunchMode.externalApplication);
  } else {
    throw '无法打开链接:$url';
  }
}

4. 平台适配配置

因为Android 11+和iOS都有隐私限制,需要额外配置才能让应用正常查询和唤起其他应用:

Android 11+ 配置

在android/app/src/main/AndroidManifest.xml中添加<queries>标签,声明你要唤起的应用包名或Scheme,比如要唤起微信和浏览器:

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- 新增这段查询权限配置 -->
  <queries>
    <!-- 允许查询微信应用 -->
    <package android:name="com.tencent.mm" />
    <!-- 允许查询HTTP/HTTPS链接对应的应用(浏览器) -->
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="http" />
    </intent>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="https" />
    </intent>
  </queries>

  <!-- 原有配置 -->
  <application ...>
    ...
  </application>
</manifest>

iOS 配置

在ios/Runner/Info.plist中添加LSApplicationQueriesSchemes数组,声明要使用的URL Scheme:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>weixin</string>
  <string>http</string>
  <string>https</string>
</array>

小提示

不同应用的URL Scheme需要提前确认,比如支付宝是alipay://,淘宝是taobao://;如果是你自己开发的其他应用,需要在那个应用的配置中提前注册好对应的Scheme,才能被正常唤起。

内容的提问来源于stack exchange,提问作者SA DL

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 18:47:51