LoginSignup
3
1

More than 5 years have passed since last update.

【Android】Deep Linkのdata設定がタスキ掛けされる件

Last updated at Posted at 2018-06-04

概要

Deep Link設定で意図しないURLが反応していたことがあったので、調べて確認しました。

事象

<data android:scheme="a2kaido" android:host="a2kaido.github.io" android:pathPrefix="/ice" />
<data android:scheme="https" android:host="a2kaido.github.io" android:pathPrefix="/cream" />

このような設定の場合に、
a2kaido://a2kaido.github.io/icehttps://a2kaido.github.io/creamだけでなく、
a2kaido://a2kaido.github.io/creamhttps://a2kaido.github.io/iceに反応してました。

そんなつもりはなかったのに。。

公式ドキュメント

公式ドキュメントにも、schemeとhostの設定がタスキ掛けされるように書かれています。
pathPrefixの設定もタスキ掛けされるんですね。

回避方法

<data>をひとつの<intent-filter>の中に書いていたところを、
分割して別々の<intent-filter>に書けばよいです。

サンプルコード

AndroidManifest.xml
<activity android:name=".MainActivity">                                                                                                              <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <!-- a2kaido://sample -->
    <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="a2kaido" android:host="sample" />
    </intent-filter>

    <!-- a2kaido://a2kaido.github.io/ice -->
    <!-- a2kaido://a2kaido.github.io/cream -->
    <!-- https://a2kaido.github.io/ice -->
    <!-- https://a2kaido.github.io/cream -->
    <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="a2kaido" android:host="a2kaido.github.io" android:pathPrefix="/ice" />
        <data android:scheme="https" android:host="a2kaido.github.io" android:pathPrefix="/cream" />
    </intent-filter>

    <!-- a2kaido://a2kaido.github.io/sand -->
    <!-- a2kaido://a2kaido.github.io/witch: no reaction -->
    <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="a2kaido" android:host="a2kaido.github.io" android:pathPrefix="/sand" />
    </intent-filter>

    <!-- https://a2kaido.github.io/witch -->
    <!-- https://a2kaido.github.io/sand: go to browser-->
    <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="https" android:host="a2kaido.github.io" android:pathPrefix="/witch" />
    </intent-filter>
</activity>

検証方法

adbコマンドでintentを送ると簡単です。

adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "a2kaido://sample"
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1