まず、URLからアプリが立ち上がるようにAndroidManifest.xmlに必要事項を追加
<!-- スキーム起動 --> <activity android:name="com.hoge.app.IntentReceiverActivity" android:screenOrientation="portrait" > <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:host="www.hoge.com" android:pathPrefix="/app/-/intent" android:scheme="http" /> </intent-filter> </activity>
これで、http://www.hoge.com/app/-/intentにアクセスしたら、該当アプリ(com.hoge.app.IntentReceiverActivityを実装したアプリ)が選択出来るようになります。
IntentReceiverActivityの実装内容は、こちら
public class IntentReceiverActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Intent intent = getIntent(); if(intent != null){ //ここでhttp://www.hoge.com/app/-/intentを取得 Uri uri = intent.getData(); //1つめのクエリをLogに出力 Log.d("sample", "query1 is : " + uri.getQueryParameter("query1")); //2つめのクエリをLogに出力 Log.d("sample", "query2 is : " + uri.getQueryParameter("query2")); } } }
最後にクエリ取得テストのために使用するコマンドがこちら
$ adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d http://www.hoge.com/app/-/intent?query1=value1&query2=value2
これで実行してログを見るとこんな感じに
query1 is value1 query2 is null
query2もあるはずなのにnullになります。
解決方法は、「&」を使うのではなく「%26」を使う事。
$ adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d http://www.hoge.com/app/-/intent?query1=value1%26query2=value2
これで正常にクエリの取得が出来るようになります。
もしくは「&」の前にバックスラッシュを「\&」
こちらの場合だと、URLを「" "」で囲う必要があります。
$ adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "http://www.hoge.com/app/-/intent?query1=value1\&query2=value2"
adb shellからテストするときは要注意ですね。
https://code.google.com/p/android/issues/detail?id=76026
stackoverflow.com