LoginSignup
54
55

More than 5 years have passed since last update.

AndroidでDeep Linkを使うときのintent-filterとdataの書き方

Last updated at Posted at 2015-10-28

アプリをWebページや他のアプリから起動するためにDeep Linkが使われています

起動したいActivityに対してAndroidManifest.xmlに以下のintent-filterを追加すると、myapp://detail?page=1のリンクによりアプリを起動することができます

AndroidManifest.xml
<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="myapp" android:host="detail" />
</intent-filter>

1つのActivityに複数のスキーム、ホストを対応させることもできます。
このとき、1つのintent-filterに複数のdataを持つか、2つのintent-filterにそれぞれdataを持つかどうかで対応するURLが変化します

Activity,intent-filter,dataの関係

Activity内に複数のintent-filterを持つとOR指定、intent-filter内に複数のdataを持つとsheme, host, pathそれぞれのAND指定となります

1つのintent-filterに2つのdata

試しに1つのintent-filterに2つのdata(片方はpathを未設定)を持つと以下のようになります

AndroidManifest.xml
<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="myapp" android:host="detail" />
    <data android:scheme="https" android:host="myapp.com" android:pathPattern="/detail.*" />
</intent-filter>

起動したURL

    myapp://detail/detail?page=1
    https://myapp.com/detail?page=1

起動しないURL

    myapp://detail/?page=1
    https://myapp.com?page=1

ここで、1つめのdataにpathPattern=".*"を追加すると、4つ全てのURLでアプリが起動します

2つのintent-filterにそれぞれ1つのdata

また、activity内に2つのintent-filterを持ち、それぞれにdataを持たせると以下のようになります

AndroidManifest.xml
<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="myapp" android:host="detail" />
</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:scheme="https" android:host="myapp.com" android:pathPattern="/detail.*" />
</intent-filter>

起動したURL

    myapp://detail/?page=1
    myapp://detail/detail?page=1
    https://myapp.com/detail?page=1

起動しないURL

    https://myapp.com?page=1

ここで1つ目のdataにpathPatternを追加するとmyapp://detail/detail?page=1のときアプリは起動しません

複数のintent-filterに対応した気になって、試すと動かなかったのでまとめました

参考:http://ja.stackoverflow.com/questions/457/%E4%B8%80%E3%81%A4%E3%81%AEintent-filter%E3%81%AB%E5%AF%BE%E3%81%97%E3%81%A6%E8%A4%87%E6%95%B0%E3%81%AEdata%E3%82%BF%E3%82%B0%E3%81%8C%E6%8C%87%E5%AE%9A%E3%81%95%E3%82%8C%E3%81%9Factivity%E3%81%AE%E5%91%BC%E3%81%B3%E5%87%BA%E3%81%97%E3%81%AB%E3%81%A4%E3%81%84%E3%81%A6

54
55
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
54
55