深层链接意图不起作用

问题描述 投票:0回答:13

我按照 https://developer.android.com/training/app-indexing/deep-linking.html 上的说明进行操作,但是当我想通过

adb
触发意图时:

adb shell am start
           -W -a android.intent.action.BROWSEABLE
           -d "http://example.com/gizmos" com.myapp.android

我刚刚得到

Error: Activity not started, unable to resolve Intent {
  act=android.intent.action.VIEW dat=example://gizmos flg=0x10000000
  pkg=com.myapp.android }

<activity
        android:name=".activities.DeepLinkActivity"
        android:label="@string/title_activity_deep_link">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />

        <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="http"
                android:host="example.com"
                android:pathPrefix="/gizmos" />
        </intent-filter>
    </activity>

我犯过任何明显的错误吗?

android android-intent android-manifest deep-linking
13个回答
137
投票

编辑:

首先确保 adb 可以访问你的包:

adb shell am start -n com.example.simon.test/.activities.MainActivity

然后,要接受多个数据标签,您需要不同的意图过滤器(这就是它对我有用的方式,与我在网上看到的所有其他示例不同)。例如:

<intent-filter>
    ...
    <data android:scheme="http"
          android:host="example.com"/>
</intent-filter>
<intent-filter>
    ...
    <data android:scheme="http"
          android:host="example.com"
          android:pathPrefix="/gizmos"/>
</intent-filter>

注意,在上面的示例中,pathPrefix 以 正斜杠 开头!

我不确定为什么 Google 的文档如此具有误导性,或者可能是针对某些不同版本的 adb,但上述更改对我来说非常有效。这有帮助:来源


这就是我如何让 Chrome 浏览器将特定链接路由到我的应用程序:

<activity
    android:name=".activities.DeepLinkActivity"
    android:label="@string/app_name">
    <!-- Accept chrome links -->
    <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="http"
              android:host="example.com"
            android:pathPrefix="/"/>
    </intent-filter>
    <!-- Accept adb data flag -->
    <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="http"
              android:host="example.com"/>
    </intent-filter>
</activity>

注意 第一个过滤器适用于 Google Chrome,而第二个过滤器适用于 ADB。

注意2 如果将链接输入到浏览器的地址栏中,则不会显示应用程序选择菜单。它 has 是某个页面侧面的

<a href="http://example.com"></a>
链接。

在我看来,这里的一切都相当模糊,而且确实不是我期望的那样。但这就是它在我的设备上的工作原理。


21
投票

经过一些测试,这对我有用:

    <activity android:name=".activities.MainActivity">
        <intent-filter android:label="@string/app_name">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="http"
                  android:host="www.example.com"
                  android:pathPrefix=""
                />
            <data android:scheme="myschema"
                  android:host="example"
                  android:pathPrefix=""
                />
        </intent-filter>
    </activity>

单击浏览器中的任何链接(例如“http://www.example.com”、“http://www.example.com/”或“http://www.example”)时,此功能有效.com/whatever”。与“myschema://example/whatever”相同。

也可以使用此命令(使用任何这些 URL)与

adb
一起使用:

adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com" com.example

希望它能帮助您入门。

当一切正常时,您可能需要为不同的活动配置不同的

pathPrefix


8
投票

就我而言,我使用的是模拟器而不是实际的 USB 连接设备,因此我必须将 -d 更改为 -e,如下所示:

adb shell am start -W -a android.intent.action.VIEW -e "http://www.example.com" com.example

注意深层链接之前的 -e


6
投票

就我而言,我将深度链接意图过滤器放在 MainActivity 中,这也是主启动器。这导致了问题。

在我创建另一个单独的活动并将意图过滤器放在那里之后,它解决了问题。希望这可以帮助其他面临同样问题的人。


3
投票

首先,阅读@user3249477在此页面上的回答。

我只是想补充一点,你可以使用 pathPattern 来压缩它,而不是他写的:

<activity
    android:name=".activities.DeepLinkActivity"
    android:label="@string/app_name">
    <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="http"
              android:host="example.com"
            android:pathPattern=".*"/>
    </intent-filter>
</activity>

“.*”同时匹配空字符串和“/”,因此两种情况都被覆盖。如果您最终尝试处理多个方案和主机,这变得尤其重要,因此您不必向 {http, https} X {"", "/"} X {"foo", "bar" 的 powerset 发送垃圾邮件等}


3
投票

确保您的网址采用以下格式(将大写字母替换为您自己的):

android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams

adb工具不需要这种格式。通过上面的内容,您现在可以将其放入 src 或 href 中,如下所示:

<iframe src="android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams"> </iframe> 
<a href="android-app://COM.YOUR.APP.IDENTIFIER/SCHEME/HOST?somegetparams">LINK</a>

2
投票

在我的例子中,我的 URL 中有一个端口 8075,我删除了它并且它起作用了


2
投票

./adb shell am start -n 包名/.splash.SplashActivity -d "schemeName://content"


1
投票

感谢 Simas 的回复,我想补充一些说明:

  • 使用此命令测试您的活动后:

    adb shell am start -n com.example.simon.test/.activities.MainActivity

  • 将意图过滤器添加到 AndroidManifest.xml 文件后,您需要测试深层链接(行如下):

    ... ...

这是您可以测试的 adb 命令:

adb shell am start -W -a android.intent.action.VIEW -d "http://example.com/gizmos" com.example.packageid

adb shell am start -W -a android.intent.action.VIEW -d "http://example.com" com.example.pakageid

1
投票

启动时使用 adb shell -W -a android.intent.action.VIEW -d“http://example.com/gizmos”com.myapp.android

它会起作用的。


0
投票

所有这些答案和我自己的解决方案都缺失:

 android:autoVerify="true"
! 一旦我将其添加到意图过滤器中,深度链接终于开始工作 =)) 这里是详细指南!


-1
投票

如果没有权限问题,可能与API级别有关


-2
投票

改用这个意图过滤器,

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

<!-- Accepts URIs that begin with "example://gizmos” -->
<data
   android:host="gizmos"
   android:scheme="example" />
</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" />

<!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
<data 
    android:host="www.example.com"
    android:pathPrefix="gizmos"
    android:scheme="http" />
</intent-filter>
© www.soinside.com 2019 - 2024. All rights reserved.