android - 深层链接 Intent 不起作用

标签 android android-intent android-manifest deep-linking

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

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>

我犯了什么明显的错误吗?

最佳答案

编辑:

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

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

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

<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,但上述更改对我来说非常有效。这有助于:Source


这就是我使 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。

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

在我看来,这里的一切都相当模糊,而且真的不是我期望的那样。但这就是它在我的设备上的工作方式。希望这对您也有帮助(并且有效)。

关于android - 深层链接 Intent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24808777/

相关文章:

android - 如何清除堆栈中的所有 Activity ?

java - 2 个 fragment 之间的 Intent 不起作用

android - 以编程方式更改后,图标未显示在 FloatingActionButton 中

android - 强制使用自定义主屏幕应用程序并且不允许默认返回

android - 在android中以编程方式启动电子邮件客户端并将电子邮件地址传递给客户端

android - 当我尝试显示对话框时,它给我强制关闭错误

android - 在 Android 中注册 C2DM 时出现问题

java - LibGDX Scene2d.ui 带倾角的自适应尺寸

android - 改造 : Getting error ** End of input at line 1 column 1**

java - USB主机: how to open my app only if it is not already in foreground?