深层链接意图不起作用

Mah*_*oni 51 android deep-linking android-manifest android-intent

我按照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
Run Code Online (Sandbox Code Playgroud)

我得到了

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>
Run Code Online (Sandbox Code Playgroud)

我有任何明显的错误吗?

Sim*_*mas 110

编辑:

好的,首先确保adb可以访问您的包:

adb shell am start -n com.example.simon.test/.activities.MainActivity
Run Code Online (Sandbox Code Playgroud)

然后接受多个数据标签你需要不同的意图过滤器(这就像我在网上看到的所有其他例子一样对我有用).例如:

<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>
Run Code Online (Sandbox Code Playgroud)

请注意,在上面的示例中,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>
Run Code Online (Sandbox Code Playgroud)

注意第一个过滤器适用于谷歌浏览器,而第二个过滤器适用于亚行.

注意2如果链接输入浏览器的地址栏,则不会显示应用程序选择菜单.它必须<a href="http://example.com"></a>某个页面的链接.

在我看来,这里的一切都相当模糊,而且我的预期并非如此.但这就是它在我的设备上的工作方式.希望这对你有帮助(也有效).

  • 很好的例子,说明我们需要不同的意图过滤器!另外,pathPrefix 中的斜杠也很重要。做得好! (2认同)

Fer*_*nch 15

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

    <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>
Run Code Online (Sandbox Code Playgroud)

单击浏览器中的任何链接(例如" http://www.example.com "," http://www.example.com/ "或" http://www.example.com/whatever ")时都可以使用此功能.与"myschema:// example/whatever"相同.

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

adb shell am start -W -a android.intent.action.VIEW -d "http://www.example.com" com.example
Run Code Online (Sandbox Code Playgroud)

希望有助于您入门.

当一切正常时,您可能希望pathPrefix为不同的活动配置不同的活动.

  • android:pathPrefix="" 路径前缀不能为空 (2认同)
  • 使用“adb”测试并不能给出真实的生产情况。例如,我想使用“myschema://example/whatever”从电子邮件打开应用程序,但 GMAIL 应用程序无法将其识别为链接。尝试将其作为链接发送(包装为链接的 href,使用 html 发送电子邮件),但 Google 将其转换为普通文本。因此,我可以启动应用程序并将其视为链接的唯一方法 - 仅使用“android:scheme=”https“”。 (2认同)

Har*_*ung 6

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

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


Pra*_*kar 5

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

adb shell am start -W -a android.intent.action.VIEW -e "http://www.example.com" com.example
Run Code Online (Sandbox Code Playgroud)

注意深层链接前的-e