Search code examples
androidandroid-intentintentfilterandroid-broadcast

Android <intent-filter> works for <activity /> but not for <receiver />


I have an Activity with multiple <intent-filter> tags:

<activity
    android:name=".NFCActivity"
    android:label="@string/title_activity_nfc" >

    <intent-filter>
        <action android:name="android.nfc.action.TRANSACTION_DETECTED" />

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

        <data
            android:host="secure"
            android:pathPrefix="/a000000004"
            android:port="0"
            android:scheme="nfc" />
    </intent-filter>

    <intent-filter>
        <action android:name="com.gsma.services.nfc.action.TRANSACTION_EVENT" />

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

        <data android:scheme="nfc" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.gsma.services.nfc.action.TRANSACTION_EVENT" />

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

</activity>

This works as expected, the activity launches as a transaction is detected.

The same thing can't be said when working with a BroadcastReceiver, the onReceive method is not invoked:

<receiver
        android:name="carta.NfcReceiver"
        android:enabled="true" >

    <intent-filter>
        <action android:name="android.nfc.action.TRANSACTION_DETECTED" />

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

        <data
            android:host="secure"
            android:pathPrefix="/a000000004"
            android:port="0"
            android:scheme="nfc" />
    </intent-filter>

    <intent-filter>
        <action android:name="com.gsma.services.nfc.action.TRANSACTION_EVENT" />

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

        <data android:scheme="nfc" />
    </intent-filter>
    <intent-filter>
        <action android:name="com.gsma.services.nfc.action.TRANSACTION_EVENT" />

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

</receiver>

am I missing something ?

Thanks


Solution

  • The same thing can't be said when working with a BroadcastReceiver, the onReceive method is not invoked:

    That is because those actions are being used by some other process in startActivity() or startActivityForResult(). You cannot respond to startActivity() or startActivityForResult() with a BroadcastReceiver.