Android深层链接自定义URI

Qui*_*don 8 android deep-linking adb

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

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

我已经定义了我的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());            
    }
}
Run Code Online (Sandbox Code Playgroud)

这符合Android文档:Android Deep Linking

所以问题是:

如何测试URI深层链接?根据文档我运行的东西

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

但这会产生:

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

我还尝试了shell,其中包含活动的名称和引用,启动器活动,并将包留空.我唯一可以上班的是:

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

但即使我明白这一点,也不是说它会在其他应用程序中运行.CUSTOM URI(例如:// gizmos)在Gmail和WhatsApp等其他应用中无法点击 - 因此在Android生态系统中进行测试也存在问题.

这个堆栈溢出问题的答案是不可接受的,因为它没有回答问题,而只是鼓励使用http://版本,我希望example:// scheme能够工作.

Qui*_*don 16

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

然后以下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上的问题(例如:// gizmos?data1 ='hello'&data2 ='world' - '&'被截断)

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

  • 谢谢!这是**在Android的参考文档中不清楚! (3认同)
  • 我有时会惊讶于公司如何如此出名,但他们的文档可能会如此模糊/误导.但这非常有用. (2认同)

Man*_*han 6

就像其他答案提到的那样,android框架要求您为在应用中启用的每个深层链接声明单独的intent-filters。这就是您收到错误的原因。

另外,您可以使用深层链接测试器应用程序直接在android上测试深层链接,而无需使用adb:

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

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

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