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

如何为Flutter应用(Android和iOS端)添加“打开方式”选项?

问题:让App出现在地图链接的打开选项中

我搜了很多资料都没解决,想让我的App出现在如图所示的打开选项里:
打开选项示例
示例链接:https://maps.google.com/?q=100.923101,100.399162
希望能提供一个小型示例应用,另外我已经在Manifest里加了以下intent-filter配置,但还是没看到我的App选项,后续还要处理传入的Intent:

<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" />
    <data android:host="www.google.com" />
    <data android:pathPrefix="/maps" />
</intent-filter>


<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="geo" />
    <data android:host="" />
</intent-filter>

解决方案

1. 修正Intent-Filter配置

你的https协议过滤规则存在两个问题:一是未匹配不带www的google.com域名,二是未明确匹配带查询参数的场景。修正后的配置如下:

<!-- 匹配https协议的谷歌地图链接 -->
<intent-filter android:autoVerify="true">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="https" />
    <!-- 同时匹配带www和不带www的域名 -->
    <data android:host="google.com" />
    <data android:host="www.google.com" />
    <!-- 匹配/maps开头的路径,兼容带参数的情况 -->
    <data android:pathPrefix="/maps" />
    <!-- 可选:精准匹配带q参数的链接 -->
    <data android:queryPattern="q=*" />
</intent-filter>

<!-- 匹配geo协议的地理坐标链接 -->
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data android:scheme="geo" />
    <data android:host="*" />
</intent-filter>

2. 小型示例应用实现

步骤1:配置Manifest文件

将上述修正后的intent-filter添加到接收Intent的Activity中(以MainActivity为例):

<activity
    android:name=".MainActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- 地图链接过滤规则 -->
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="https" />
        <data android:host="google.com" />
        <data android:host="www.google.com" />
        <data android:pathPrefix="/maps" />
        <data android:queryPattern="q=*" />
    </intent-filter>

    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="geo" />
        <data android:host="*" />
    </intent-filter>
</activity>

步骤2:处理传入的Intent

在MainActivity中解析Intent里的坐标数据:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        handleIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        handleIntent(intent)
    }

    private fun handleIntent(intent: Intent?) {
        intent ?: return
        if (Intent.ACTION_VIEW != intent.action) return

        when (intent.scheme) {
            "https" -> {
                // 解析谷歌地图链接的坐标参数
                intent.data?.getQueryParameter("q")?.split(",")?.let {
                    if (it.size == 2) {
                        val latitude = it[0].toDoubleOrNull()
                        val longitude = it[1].toDoubleOrNull()
                        // 这里添加你的业务逻辑,比如显示坐标
                        Log.d("MapIntent", "解析到坐标:纬度$latitude,经度$longitude")
                    }
                }
            }
            "geo" -> {
                // 解析geo协议的坐标
                intent.dataString?.substringAfter("geo:")?.split(",")?.let {
                    if (it.size >= 2) {
                        val latitude = it[0].toDoubleOrNull()
                        val longitude = it[1].toDoubleOrNull()
                        Log.d("MapIntent", "解析到Geo坐标:纬度$latitude,经度$longitude")
                    }
                }
            }
        }
    }
}

3. 测试注意事项

  • 确保App安装在设备上,Debug包或签名包均可,部分设备可能需要重启后生效
  • 若测试时未显示App选项,检查设备是否将地图链接默认绑定到了其他App,可进入应用设置清除对应App的默认打开方式
  • 用ADB命令快速测试:
    adb shell am start -a android.intent.action.VIEW -d "https://maps.google.com/?q=100.923101,100.399162"
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 08:18:27