Reputation: 5183
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
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:
Activity
Service
BroadcastReceiver
The PendingIntent
class offers 3 different methods to get a PendingIntent for each of these different purposes:
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