6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Intent-Filterでpathありとpathなしを一瞬で見分ける方法

6
Last updated at Posted at 2016-07-02

AndroidManifest.xmlでschemeとhostを指定するとき、
myapp://myhost/mypathmyapp://myhost を見分けるのに地味に困りました。

この問題は正規表現で一発解決しました。

「myapp://myhost/mypath」

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:pathPattern="mypath"
             android:scheme="myapp" />
    </intent-filter>

「myapp://myhost」

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:scheme="myapp" />
    </intent-filter>

↑ これだとmyapp://myhost/mypathmyapp://myhost の両方に反応してしまう。

    <intent-filter>
         <action android:name="android.intent.action.VIEW" />
         <category android:name="android.intent.category.DEFAULT" />
         <data
             android:host="myhost"
             android:pathPattern="\s"
             android:scheme="myapp" />
    </intent-filter>

↑ そこで空文字を表す正規表現の\sを使うとmyapp://myhost にしか反応しなくなる。

これでRESTFULなスキーマにも対応できる。

6
5
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
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?