深度链接时奇怪的Android intent-filter pathPattern问题

时间:2016-02-04 10:26:16

标签: android pattern-matching deep-linking

我想为这两个http网址

创建深层链接
  

https://sample-app-123.appspot.com/backoffice/#/user_reset_password?email=abc@gmail.com   https://sample-app-123.appspot.com/backoffice/#/user_create_password?email=abc@gmail.com

我已经完成了各种StackOverflow答案,但都没有奏效。我知道Android使用 PatternMatcher.PATTERN_SIMPLE_GLOB 来匹配模式而不是正则表达式。

我的清单看起来像

<activity android:name=".CreateActivity"
    android:label="Create Password"
    >
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data
        android:host="sample-app-123.appspot.com"
        android:scheme="https"
        android:pathPattern=".*create_password.*"
        />
    </intent-filter>

</activity>
<activity android:name=".ResetActivity"
    android:label="Reset Password"
    >
    <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data
        android:host="sample-app-123.appspot.com"
        android:scheme="https"
        android:pathPattern=".*reset_password.*"
        />
    </intent-filter>
</activity>

我已经创建了几个用于检查 PatternMatcher.PATTERN_SIMPLE_GLOB 的测试,所有测试都已通过,我无法解释为什么它无法正常工作

public void testPatternMatcher() throws Exception {
    PatternMatcher mPatternMatcher;

    mPatternMatcher = new PatternMatcher("https://sample-app-123.appspot.com/backoffice\\/#\\/user_create_password.*", PatternMatcher.PATTERN_SIMPLE_GLOB);
    assertTrue(mPatternMatcher.match("https://sample-app-123.appspot.com/backoffice/#/user_create_password?email=abc@gmail.com"));
    assertFalse(mPatternMatcher.match("https://sample-app-123.appspot.com/backoffice/#/user_reset_password?email=abc@gmail.com"));

    mPatternMatcher = new PatternMatcher(".*user_create_password.*", PatternMatcher.PATTERN_SIMPLE_GLOB);
    assertTrue(mPatternMatcher.match("https://sample-app-123.appspot.com/backoffice/#/user_create_password?email=abc@gmail.com"));
    assertFalse(mPatternMatcher.match("https://sample-app-123.appspot.com/backoffice/#/user_reset_password?email=abc@gmail.com"));

    mPatternMatcher = new PatternMatcher(".*user_reset_password.*", PatternMatcher.PATTERN_SIMPLE_GLOB);
    assertFalse(mPatternMatcher.match("/backoffice/#/user_create_password?email=abc@gmail.com"));
    assertTrue(mPatternMatcher.match("/backoffice/#/user_reset_password?email=abc@gmail.com"));
}

1 个答案:

答案 0 :(得分:2)

create_passwordreset_password不属于uri路径的一部分 uri路径在#字符之前结束。这是一个特殊字符,用于从query(或锚点)界定hash

方案:https://
主持人:sample-app-123.appspot.com
路径:/backoffice/
片段#/user_reset_password?email=abc@gmail.com

您应该使用以下网址:

https://sample-app-123.appspot.com/backoffice/user_reset_password?email=abc@gmail.com
https://sample-app-123.appspot.com/backoffice/user_create_password?email=abc@gmail.com

这将按预期工作。

方案:https://
主持人:sample-app-123.appspot.com
路径:/backoffice/user_create_password
查询:email=abc@gmail.com