In part 1, we have discussed the need and use of pending intents with an example. We have created PendingIntent
and set PendingIntent.FLAG_ONE_SHOT in example but we haven’t discussed anything about creation and flags. These are the topics
of this article.
Creating Pending Intents
Pending Intent class provides static methods for creating pending intent of desired type. Like any other intent,
we create pending intent for specific component. e.g. activity, service etc.
Note: There are other methods too, which you can explore on your own.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it.
This means that, even if its owning application’s process is killed, the PendingIntent itself will remain usable
from other processes that have been given it. If the creating application later re-retrieves the same kind of
PendingIntent (same operation, same Intent action, data, categories, and components, and same flags),
it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.
From the very first line, it’s clear that Pending Intents are stored somewhere in the system and can be updated later. These Pending Intent Flags
help us to achieve this behaviour. Depending on the flag passed to create pending intent we can update or change the behaviour of intent.
Let’s discuss each of them briefly.
If a pending intent is created using PendingIntent.FLAG_ONE_SHOT flag; it can be used only once. Any attempt to reuse it will fail (throw
I will create a simple app here.
SampleService - waits for 3 seconds and execute the action attached to pending intent.
MainActivity - main activity, it has an init button which starts the service and schedule starting it again after 5 seconds from now.
ResultActivity - activity which will be started from service. It logs the extra data carried with intent.
In this example, we have create pending intent with FLAG_ONE_SHOT
When the same intent is passed to starting service in scheduled called in Handler, service throws CanceledException becuase
this intent was supposed to be used only once.
This flag indicates that if intent is already there, update its extras with new intent’s extras otherwise return a new pending intent.
Update onInit method with following code and analyze the result.
It indicates that if described pending intent already exists, cancel it before creating new one. In our example, service waits
for 3 seconds before starting new activity. We will update the onInit() method, now it will schedulea new service start after 1
second. It will cancel the previous one (generation canceled exception in the service) and will start a new one.
Analyze the output carefully.
This flag returns null if it does not find any existing pending intent to return.
Update onInit() with this code and see the output.
FLAG_IMMUTABLE (API >= 23)
Flag indicating that the created PendingIntent should be immutable.
This means that the additional intent argument passed to the send methods to
fill in unpopulated properties of this intent will be ignored.
Note: I haven’t used this flag yet. I have added it here just for completion. You can explore it on your own or I will update it here
whenever I get time.