gwvatieri
gwvatieri

Reputation: 5183

Intent's flags and PendingIntent.getBroadcast

I get this exception in my code:

...IllegalArgumentException...Cant use FLAG_RECEIVER_BOOT_UPGRADE here...

Looking into the android source code seems like you can't set flags to an Intent that will be fired through:

PendingIntent.getBroadcast(...);

Here the Android source code:

...
if (type == INTENT_SENDER_BROADCAST) {
    if ((intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0) {
        throw new IllegalArgumentException("Can't use FLAG_RECEIVER_BOOT_UPGRADE here");
    }
}
...

Here my code:

Intent myIntent = new Intent(context, MyReceiver.class);
//myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if i remove the comment it doesn't work
PendingIntent pending = PendingIntent.
          getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);

The reason is not clear to me, anyone could clarify it for me please?

Upvotes: 1

Views: 5908

Answers (1)

David Wasser
David Wasser

Reputation: 95628

When you get a PendingIntent using getBroadcast(), this Intent will be broadcast to BroadcastReceivers. It will NOT be used to start an Activity. So you cannot set any of the Activity-related flags. They wouldn't make any sense in that context anyway.

Why would you want to set FLAG_ACTIVITY_NEW_TASK in an Intent that will be broadcast? That makes no sense.

Android uses Intents for 3 completely different purposes:

  1. Starting/Communicating with Activity
  2. Starting/Communicating with Service
  3. Broadcast to BroadcastReceiver

The PendingIntent class offers 3 different methods to get a PendingIntent for each of these different purposes:

  1. getActivity()
  2. getService()
  3. getBroadcast()

You need to make sure that you use the right method for the right purpose.

And yes, you can can set Activity-related Intent flags in a PendingIntent, as long as you call getActivity() to get the PendingIntent.

Upvotes: 5

Related Questions