【问题标题】:Android Deep Linking with custom URI带有自定义 URI 的 Android 深度链接
【发布时间】:2015-12-24 09:53:50
【问题描述】:

我的清单中定义了以下内容:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.package">
...
    <activity
        android:name="app.myActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <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:host="www.example.com"
                android:pathPrefix="/gizmos"
                android:scheme="http" />
            <!-- note that the leading "/" is required for pathPrefix-->
            <!-- Accepts URIs that begin with "example://gizmos”-->
            <data
                android:host="gizmos"
                android:scheme="example" />
        </intent-filter>
    </activity>
 ...

我已经这样定义了我的 onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    Uri data = intent.getData();
    if (data != null) {
        Log.d("URI",data.toString());            
    }
}

这是根据 Android 文档:Android Deep Linking

所以问题是:

如何测试 URI 深层链接?根据文档,我运行类似

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

但这会产生:

错误:Activity 未启动,无法解析 Intent { act=android.intent.action.VIEW dat=example://gizmos flg=0x10000000 pkg=com.app.package }

我还尝试了带有活动名称和引用的外壳,启动器活动并将包留空。我唯一能去上班的是:

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

但即使我做到了,这也不是说它可以在其他应用程序中工作。自定义 URI(例如 example://gizmos)在 Gmail 和 WhatsApp 等其他应用中不可点击 - 因此在 Android 生态系统中进行测试也存在问题。

at this stack overflow question 的答案是不可接受的,因为它不回答问题,而只是鼓励使用 http:// 版本,我希望 example:// 方案能够工作。

【问题讨论】:

  • 请不要试图将多个问题塞进一个帖子中。即使你认为它们是相关的。

标签: android adb deep-linking


【解决方案1】:

ADB 正在发送意图,只是当您需要多种链接类型时,您的应用必须具有单独的意图过滤器:

    <activity
        android:name="app.myActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <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" />
            <data
                android:host="www.example.com"
                android:pathPrefix="/gizmos"
                android:scheme="http" />
            <!-- note that the leading "/" is required for pathPrefix-->                
        </intent-filter>
    </activity>

那么下面的 ADB 命令都可以工作:

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

但是我遇到了将更多元素附加到自定义 URI 的问题(例如 example://gizmos?data1='hello'&data2='world' - '&' 被截断)

显然,这并不能解决这些链接在其他应用程序(如 WhatsApp 和 Gmail)中不可点击的事实......但它们在其他平台上是可点击的。

【讨论】:

  • 谢谢!这在 Android 的参考文档中SO不清楚!
  • 我有时会惊讶于一家公司为何如此知名,但他们的文档却如此晦涩/误导。不过,这很有帮助。
  • 如何从浏览器打开example://gizmos?命令adb shell am start -W -a android.intent.action.VIEW -d "example://gizmos" 工作正常。
  • 我认为这取决于浏览器 - 例如,我知道(根据历史经验,因此可能已经改变)WhatsApp 只是不转发深层链接 - 或者更确切地说,不广播它们。您可能想在这里寻找潜在的解决方案:stackoverflow.com/questions/35175055/…
【解决方案2】:

正如另一个答案所提到的,android 框架要求您为您在应用中启用的每个深层链接声明单独的意图过滤器。这就是您收到错误的原因。

另外,您可以使用深度链接测试器应用直接在 android 上测试深度链接,而不是使用 adb:

https://play.google.com/store/apps/details?id=com.manoj.dlt

无需提及任何包名或组件名。只需输入实际的深层链接并触发。

我使用过深层链接,就个人而言,我发现通过 adb 进行测试非常耗时。因此,我创建了这个应用程序来直接测试深层链接并将其放在 Play 商店中。

【讨论】:

    【解决方案3】:

    airbnbDeepLinkDispatch 库。解决您处理DeepLink的所有后顾之忧。

    图书馆的功能如下:

    • 易于使用
    • 处理所有类型的自定义 URL
    • 链接参数中的句柄
    • 处理查询参数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 2021-05-15
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      相关资源
      最近更新 更多