PendingIntent in Android Application Development

SHISHIR
PROGRAMMING LITE
Published in
3 min readMay 27, 2018

Let’s say you want to take shower. But you want to shower after you brush my teeth and eat breakfast. So you know you wont be showering until at least 30–40 minutes. You still have in your head that you will need to prepare my clothes, then walk up to the stairs back to the bathroom and then shower.

However this will happen until 30–40 minutes have passed. Then you now have an pendingIntent to shower. And it is pending for 30–40 minutes.

The moral of the story is this, PendingIntent is an intent that will perform at a later time or in other words PendingIntent specifies an action to take in future. The main differences between a pendingIntent and regular intent is pendingIntent will perform at a later time where Normal/Regular intent starts immediately.

There are many action that can be done with pendingIntent like sending email, attaching a photo to an email even start a activity, services or send Broadcast like a regular intent.

A pendingIntent wraps a regular intent that can be passed to a foreign application (like AlarmManger or NotificationManager etc..) where you are granting that application the right to perform the action. Normally a pendingIntent is used with AlarmManager or NotificationManager.

PendingIntent must need a regular intent to be created. But Regular intent does not require pendingIntent.

A pendingIntent uses the following method to handle different types of intent like

PendingIntent.getActivity() — This will start an Activity like calling context.startActivity()PendingIntent.getBroadcast() — This will perform a Broadcast like calling context.sendBroadcast()PendingIntent.getService() — This will start a Services like calling context.startService()PendingIntent.getForegroundService() — This will start a foregroundService like calling context.startForegroundService()

PendingIntent and its workflow

First of all you need to create an intent with required information for creating pendingIntent. Then the created pendingIntent will be passed to a target application (like AlarmManager, NotificationManager etc) using target application services. Then the target application will send the intent exists in pendingIntent by calling send on pendingIntent.

Let’s get into some code now. We’ll create an Intent and wrap it into a PendingIntent:

//Creating a regular intentIntent intent = new Intent(this, SomeActivity.class);// Creating a pendingIntent and wrapping our intentPendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)

So we created an intent, wrapped it inside a PendingIntent. Let’s go through the arguments passed to getActivity().

this (context) — This is the context in which the PendingIntent should start the activity.

1 (requestCode) — This is private code for the sender. In case of cancelling the pendingIntent we will need this code.

intent(intent) — Explicit intent of the activity to be launched.

flag : — One of the PendingIntent flag that we’ve used in the above example is FLAG_UPDATE_CURRENT.

There are many other flags also. All are discussed below.

FLAG_CANCEL_CURRENT:

If described pendingIntent already exists then current one should be cancelled before generating new one.

You can use this when you are changing the extra data in the intent by cancelling the previous intent. This flag will ensure that only the given new data will be able to launch it. If assurance is not issue Then FLAG_UPDATE_CURRENT can be used.

FLAG_UPDATE_CURRENT:

If described pendingIntent already exists then keep it and replace with extra data with what is in this new intent.

If you don’t care that any entities that received your previous intent then you can use this.

FLAG_ONE_SHOT:

This pendingIntent can be used only once.

FLAG_IMMUTABLE:

This pendingIntent should be immutable.

Now let’s see a better usage of pendingIntent with AlarmManager.

// Getting AlarmManager Services and schedule it to go off after 3 seconds…….AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + ( 3 * 1000 ), pendingIntent)

We get the alarm manager to which we passed the pendingIntent and specify it to go off in 3 seconds. So after 3 seconds Alarm Manager will send the intent exists in pendingIntent by calling send on pendingIntent. And the intent will start a activity named SomeAcitvity(). This will must be happened weather our application is running or not. Because the responsibility of starting SomeActivity() we gave to alarm manager through pendingIntent.

Also we can send broadcast through pendingIntent using alarm manager at a specific time of the day for sync data or any work you wish required for your application. In this case you have to create a Broadcast receiver for catching the intent and for doing your work.

So hopefully you understood how an Intent can be passed to other application via PendingIntent for execution. Hope that helps!

Thanks for reading. Happy Coding !

--

--

SHISHIR
PROGRAMMING LITE

{ 'designation' : 'Lead Software Engineer' , 'hobby' : [ 'Music', 'Photography', 'Travelling' ] ,’email’: ‘shishirthedev@gmail.com’ }