首页 文章

Android深层链接自定义URI

提问于
浏览
7

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

<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

所以问题是:

How do I test the URI deep linking? According to the documentation I run something like

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

但这会产生:

错误:活动未启动,无法解析Intent

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

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

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

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

2 回答

  • 3

    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 /小玩意”

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

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

  • 12

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

    此外,您可以使用深层链接测试器应用程序直接在Android上测试深层链接,而不是使用adb:

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

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

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

相关问题