4

複数の暗黙的インテントに対応したアクティビティを起動したいケースがありますが、 指定したIntentの発行では起動できずこまっております。

起動したいアプリ(アクティビティ)のAndroid Manifest.xmlを以下のように設定されています。

<activity
    android:name="com.sample.testapp.MainActivity"
    android:exported="true"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />

        <data
            android:scheme=“testapp" />
        <data
            android:host=“hoge"
            android:pathPrefix=“/fuga"
            android:scheme="https" />
    </intent-filter>
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
 </activity>

上記のとおり”http”とカスタムスキーム”testapp"の2つのdataタグを設定されているのですが、 この時、”testapp”のスキームが反応しません。

adb shell am start -a android.intent.action.VIEW -d "testapp:”

起動のIntentは上記のように指定しております。

Starting: Intent { act=android.intent.action.VIEW dat=testapp: } Error: Activity not started, unable to resolve Intent { act=android.intent.action.VIEW >dat=testapp: flg=0x10000000 }

エラーは上記のようになっております。

このようなfilterを実装をしているActivityに対しては、どのような方法でアクティビティを呼び出すのが正しいのでしょうか? 有効な手段をご存じの方がいらっしゃいましたら教えて下さい。

2 件の回答 2

5

intent-filterにおけるdataの扱いに誤解がありそうです。 1つのintent-filter内に複数のdata要素がある、このサンプルコードの場合、

<intent-filter>
    ...省略...
    <data
        android:scheme="testapp" />
    <data
        android:host="hoge"
        android:pathPrefix="/fuga"
        android:scheme="https" />
</intent-filter>

一見すると、data要素ごとにOR指定のように読めますが、 実際の動作は「scheme属性はtestappまたはhttpsを指定 AND host属性はhoge AND pathPrefix属性は/fugaである」です。サンプルコードでActivityを呼び出す場合には、

adb shell am start -a android.intent.action.VIEW -d "testapp://hoge/fuga"

または

adb shell am start -a android.intent.action.VIEW -d "https://hoge/fuga"

で起動できます。

data要素の詳細は http://developer.android.com/guide/topics/manifest/data-element.html が詳しいです。

1

本家SOに似たような質問と回答がありました。

<intent-filter> を分けてみてはどうでしょうか。

<activity
  android:name="com.sample.testapp.MainActivity"

  android:exported="true"
  android:label="@string/app_name" >
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />

    <data
      android:scheme="testapp" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.BROWSABLE" />
    <category android:name="android.intent.category.DEFAULT" />

    <data
      android:host="hoge"
      android:pathPrefix="/fuga"
      android:scheme="https" />
  </intent-filter>
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
2
  • ご回答ありがとうございます。説明が不足しておりましたが、呼び先のアプリケーションについては、すぐには変更が難しく、呼び出し元(Intentの発行元)でどうにか方法がないかと探していた次第です。
    – inon29
    2014年12月9日 0:25
  • 呼び出し側で対応するのであれば、 data 部分を testapp://hoge/fuga にすれば呼び出せると思います
    – zaki50
    2014年12月9日 6:15

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。