Android Delegation and Callbacks through PendingIntents


Welcome to part 3 this review of the Pluralsight course Android Programming with Intents by Jim Wilson

Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.

After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at

Android Programming with Intents is the 6th course in the Pluralsight learning path for Android, and this month I am reviewing every course in this learning path.

Delegation and Callbacks through PendingIntents

The role of PendingIntents

Jim says PendingIntents complete the circle of intent-based programming.

We use Intents to enable our components to take advantages of other program components.

We also want other programs to take advantage of our components. PendingIntents enable this, providing a safe way to delegate actions.

There are typically three steps involved:

  1. Create an Intent
  2. Used getXXX static method to wrap the Intent in a PendingIntent
  3. Pass the PendingIntent to the component/platform feature

Jim says this is very simple. We create the Intent that we want to have sent, and then wrap it in an action with a call to a method such as getActivity, getService or getBroadcast.

We can then pass the PendingIntent over to the other component. And voila!

Demo: The role of PendingIntents

We have a very simple application with two activities:

  1. IntentTargetActivity, with onCreate and showIntentInfo methods
  2. MyActivity, the main activity with createAndAddNotification method

We see that whenever we create a notification we need to pass in a PendingIntent.

Be aware that notification.setLatestEventInfo, which is used here, has been removed in API Level 23.

Halfway into this lesson, we see this running in the emulator. Jim clicks the “Create Pending Intents” button and we see a “Pending Intent 1” notification appears at the top.

The user can click on this notification, and the other activity will be run.

Jim shows us the manifest and we see the target activity is exposed with an action of SHOW_TEST_ACTIVITY.

Jim also demonstrates that we can create an implicit intent that uses this action:

private PendingIntent createPI2(){
    Intent intent = new Intent("com.pluralsight.SHOW_TEST_ACTIVITY");
    PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
return pi;

Now when Jim runs this updated program in the emulator and clicks “Create Pending Intent” we see two notifications.

When we choose the second notification, we see the associated action is displayed.

The contents of a PendingIntent

A PendingIntent encapsulates all of the following:

  • wrapped Intent
  • associated action
  • creating component’s security identity & rights
  • creating component’s package

We don’t no what the security rights of the other component might be. So that the system can be sure that it will be able to perform the desired action, all security credentials and rights are packaged inside the pending intent.

The presence of the component’s package means a pending intent can still fire even if the original component is shut down.

So how are we protected against a 3rd party component doing something malicious with our security information?

The contents of a PendingIntent are invisible to other apps. The contents are never shared and are only stored as an entry within the Android system.

Demo: The contents of a PendingIntent

Jim uses the debugger to show us what we can find inside a PendingIntent object.

All we see for the property mObject is a six digit number.

When the Intents are the same, the object numbers are the same.

PendingIntent sharing

PendingIntents are references, and this allows the system to share the entries. So we can have several pending intents that all point to the same mObject.

Pending intents are only shared within the same application.

Jim says Intents have a method called filterEquals, and this is used to find equivalent Intents, i.e. do they pass the same test? If so, the reference is shared.

We also learn more about the PendingIntent flags here. They give us a lot of control over the way that getting pending intents work. Jim describes the following flags:

The official documentation for PendingIntent also covers several other flags, so look there if these common flags don’t give you what you need.

Demo: PendingIntent sharing

Continuing from the previous demo we have two PendingIntents. Jim adds extras to one of them.

When run in the emulator, we see that this information is lost: because the PendingIntents match, they share the same reference and we lose our extras.

We can protect ourselves from having this information lost by using flags. Jim uses FLAG_UPDATE_CURRENT which updates the information to match the information we are passing in with the new intent.

When we run this up, we see we have our extras again:

** Extras **

Jim also demonstrates the equals method. This compares two PendingIntent objects, such that true is returned then they both represent the same operation from the same package.

Sending a PendingIntent

Most of the time we create a pending intent and pass it off to another component to use.

But sometimes we want to send the pending intent, i.e. perform the action inside of a pending intent that another component created.

We do this with the send() method.

Demo: Sending a PendingIntent

Jim says when dealing with control panel entries that belong to a service we may need deeper access than we have available directly from an Intent.

We can make the services control panel operate under the credentials of the service itself.

We start with an empty method showWallpaperControlPanel and a pre-written method called getWallPaperServiceComponentName.

The code written in this lesson allows us to select the wallpaper we like.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s