1

What is the use of the category "android.intent.category.DEFAULT" with respect to broadcast receiver?

    <receiver
        android:name=".receivers.InputReceiver"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="com.amazon.intent.action.CUSTOM_A" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

Android documentation says that to match any implicit intents add the category default.

Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare it in your intent filter, no implicit intents will resolve to your activity.

After Android-O, I will not be able to send implicit broadcasts. Does that mean "CATEGORY_DEFAULT" will not be useful with broadcasts?

I'm quite confused with the use of CATEGORY_DEFAULT with broadcast and activity. Can someone clarify me please? What is the right scenario to use CATEGORY_DEFAULT and what would happen if I didn't add it?

gooogle
  • 375
  • 4
  • 16

1 Answers1

4

What is the right scenario to use CATEGORY_DEFAULT

When you call startActivity() or startActivityForResult(), if the Intent has no categories in it, the framework will add android.intent.category.DEFAULT to the Intent, before trying to identify the component that should respond to that Intent. This is why an <intent-filter> for an <activity> usually has a <category> element, either for android.intent.category.DEFAULT or for some other category.

When you call startService(), bindService(), or sendBroadcast(), if the Intent has no categories in it, the framework leaves the Intent alone. This is why an <intent-filter> for a <service> or <receiver> often lacks a <category> element, as no category is required.

and what would happen if I didn't add it?

If an Intent contains a category, and your <intent-filter> does not contain the category, your <intent-filter> will not match the Intent. This is why we normally need a <category> with <activity>, since the Intent probably has a category on it.

In the case of com.amazon.intent.action.CUSTOM_A from your <receiver> in your code snippet, apparently Amazon elected to add android.intent.category.DEFAULT to their broadcast Intent manually. That's odd but it is allowed by the SDK.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so, do I need add a DEFAULT category for a receiver? For a BOOT_COMPLETE receiver, for example? It's just part about receivers sounds ambiguous – Johnny Five Dec 20 '19 at 13:37
  • @JohnnyFive: "do I need add a DEFAULT category for a receiver?" -- generally, no, but it depends on how the `Intent` is constructed that is used for the broadcast. "For a BOOT_COMPLETE receiver, for example?" -- generally, you do not need a `` for that. I cannot rule out some device manufacturer screwing something up, though. – CommonsWare Dec 20 '19 at 13:45
  • "I cannot rule out some device manufacturer screwing something up, though." That is the 2nd time I encounter this assumption. First was here https://stackoverflow.com/a/29836141/6325722 Is there any logic behind this assumption or it's just an intuition? – Johnny Five Dec 20 '19 at 13:51
  • @JohnnyFive: "Is there any logic behind this assumption" -- device manufacturers tweak Android all the time, in ways that sometimes introduce incompatibilities. – CommonsWare Dec 20 '19 at 14:14