scheme でアプリケーションを起動して引数を受け取る(Android編)

Androidの場合は以下のページが解り易いと思う。

Android の場合は Intentでアプリケーションを起動するので、AndroidManifest.xmlの設定をする必要がある。

<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="paraches" android:host="paraches.com"  android:pathPrefix="/openwith" />
</intent-filter>

最初の はデフォルトで入っているもの。


次の scheme使って起動する際のもの。
ブラウザからのリンクを想定しているので に BROWSABLE が入ってる。
では scheme, host, pathPrefix をそれぞれ指定している。
この例では3つとも指定しているけど、例えば scheme だけを指定しても大丈夫。その場合は hostや pathPrefixがどんな値でもアプリが起動する。


で、アプリケーションが起動すると Activity の onCreate が呼び出されるのだけど、そこで URL のデータを取り出すにはこんな感じで。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Intent intent = getIntent();
    String action = intent.getAction();
    if (Intent.ACTION_VIEW.equals(action)) {
    	Uri uri = intent.getData();
    	if (uri!=null) {
    		String name = uri.getQueryParameter("name");
    		String url = uri.getQueryParameter("url");
            ...
        }
    }
}

Android だと get.QueryParameter() なんてのがあってサックリと引数をゲットできて楽チン!


Androidのサンプルはこちら。