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

Kivy应用配置自定义URI Intent后无法被浏览器唤起的问题

Kivy(Buildozer)深度链接无法唤起应用问题排查

问题描述

我尝试让Buildozer构建的Kivy应用支持深度链接,希望通过浏览器重定向唤起应用并传递数据。已注释Buildozer配置中的android.manifest.intent_filters = intent_filters.xml行,创建了intent_filters.xml文件,内容如下:

<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="test_app" android:host="set_intro_text"/>
</intent-filter>

同时编写了Python代码获取Intent数据:

from jnius import autoclass
..
def on_start(self):
    # Register for the intent when app is started
    activity = autoclass('org.kivy.android.PythonActivity').mActivity
    intent = activity.getIntent()
    data = intent.getData()

    # Check if the app was launched with a specific URL scheme
    if data:
        self.user_label.text = f'Hello from outside> {data.getScheme()}'

但在Chrome或三星互联网浏览器中输入test_app://set_intro_text?data="opened from chrome"时,浏览器将其当作搜索请求跳转至默认搜索引擎,无法唤起应用。

原因及解决步骤

1. Buildozer配置未启用Intent Filter

你注释了android.manifest.intent_filters = intent_filters.xml这行,导致Buildozer打包时完全忽略了你的intent_filters.xml文件,系统根本不会注册这个深度链接规则。

  • 解决: 取消注释该行,确保Buildozer将intent filter注入到AndroidManifest.xml中。

2. 浏览器直接输入Scheme链接的限制

Chrome等现代浏览器为了安全,直接在地址栏输入自定义scheme链接(如test_app://xxx)时,会默认当作搜索词处理,不会直接唤起应用。

  • 验证/解决:
    • 改用HTML超链接测试:创建一个简单的HTML文件,添加<a href="test_app://set_intro_text?data=opened_from_chrome">打开应用</a>,用浏览器打开该文件后点击链接。
    • 用adb命令验证intent规则是否生效:
      adb shell am start -W -a android.intent.action.VIEW -d "test_app://set_intro_text?data=test" 你的应用包名
      
      如果adb能唤起应用,说明intent配置本身没问题,只是浏览器输入方式的限制。

3. Intent Filter的Data匹配范围不足

当前intent filter中仅指定了android:host="set_intro_text",虽然你的链接理论上能匹配,但补充android:pathPattern=".*"可确保所有子路径、参数都能被正确匹配:

<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="test_app" 
          android:host="set_intro_text"
          android:pathPattern=".*"/>
</intent-filter>

4. 应用缓存或旧版本干扰

如果之前安装过未配置intent filter的旧版本,系统可能缓存了旧的应用信息。

  • 解决: 完全卸载旧应用,重新打包安装最新版本。

补充:参数获取代码优化

当前代码仅获取了scheme,若要提取链接中的data参数,可修改代码:

from jnius import autoclass, cast
from android.net import Uri

def on_start(self):
    activity = autoclass('org.kivy.android.PythonActivity').mActivity
    intent = activity.getIntent()
    data = intent.getData()
    if data:
        uri = cast('android.net.Uri', data)
        # 获取data参数值
        param_data = uri.getQueryParameter("data")
        if param_data:
            self.user_label.text = f'Hello from outside> {param_data}'
        else:
            self.user_label.text = f'Hello from outside> {data.getScheme()}'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 09:42:34