woshidan's blog

あいとゆうきとITと、とっておきの話。

AndroidのIntentFilterのdata要素のpathPatternの挙動について

http://developer.android.com/intl/ja/guide/topics/manifest/data-element.html

data要素のURIでpathPatternの指定が出来るのですが、Patternと言いつつ、 そのパスが一致するか、*のワイルドカードを使ってその階層が入るか、くらいしか指定できず、 android:pathPrefix="/"の指定を入れると、挙動がよく分からない感じになったのでややつらかった。

たぶん、正規表現を使えるようにすると、端末内のアプリを全部検索するには重すぎるのでは、 という気が若干してなんともいえないですが、Google先生頑張って...。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.woshidan.uriintentfilter" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </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="http"
                    android:host="example.woshidan.com"/>

                <!--<data android:scheme="http"-->
                    <!--android:host="example.woshidan.com"-->
                    <!--android:pathPattern="/intentfilter/.*" />-->
                <!-- 上記で http://example.woshidan.com/intentfilter/xxxx/yyy...に反応 -->
                <!-- httpsの場合はもう1個intent-filterを同じように書く必要がある -->

                <!--<data android:scheme="http"-->
                    <!--android:host="example.woshidan.com"-->
                    <!--android:pathPattern="/.*/.*" />-->
                <!-- 上記で http://example.woshidan.com/xxxx/yyy...にのみ反応 -->

                <!-- 混乱の原因 -->
                <!--<data android:scheme="http"-->
                    <!--android:pathPrefix="/"-->
                    <!--android:host="example.woshidan.com"-->
                    <!--android:pathPattern="/.*/.*" />-->
                <!-- 上記で Prefixが入るとすべてのPathに反応するような気が... -->
            </intent-filter>
        </activity>
    </application>
</manifest>