ZOFTINO.COM android and web dev tutorials

Android Pending Intent and Uses

Android applications communicate using intents to perform some action. Intent is an object that carries required information to perform destination-component action. Activity, service and broadcast receiver components can handle intents. Intents can start activity in the same application or foreign applications.

Android provides a special object PendingIntent which is a wrapper object around Intent object. PendingIntent is not used to start a component in target application. It is used to give permission to foreign application to start at later point of time a component in your application with intent passed in pending intent. Your application sends pending intent to target application. On certain event in foreign application, activity associated with the intent that exists in the pending intent, will be started.

How to send pending intent?

First, in one of your component, in activity, service or broadcast receiver, where you need pending intent, you need to create Intent object with information required to start an activity in your application. Then using the intent object you can create pending intent object. Then the created PendingIntent object can be passed to target application using target application service from your application. On occurrence of an event, in the target application, which pending intent is tied to, target application sends the intent exists in pending intent, by calling send on PendingIntent.

Below code shows communication between two applications using pending intent. First, source application calls a method on system service of target application passing an object with pending intent tied to an event in target application.

//create intent and pending intent
Intent eventIntent = new Intent(this, EventHandlerActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, eventIntent, 0);

//create your second app event builder and set pending event which would be tied to some event in the second app
TargetAppEventBuilder tavBuilder = new TargetAppEventBuilder(this);
tavBuilder.setEventIntent(pendingIntent );

//get your second app system service and use it
TargetAppEventManager targetAppEventManager =
    (TargetAppEventManager) getSystemService(Context.TARGETAPaP_SERVICE);

It is very rare that the need for creating a system service is required. There are existing system services which can be used to send pending intent to accomplish certain flow for your application, NotificationManager, AppWidgetManager and AlarmManager. I am going to show how to use pending intent with NotificationManager. You application sends notification, user clicks the notification text, this click event sends intent (from Pendingintent) to start activity in your application.

 NotificationCompat.Builder noteBuilder =
        new NotificationCompat.Builder(this);

noteBuilder.setContentTitle("Showing Pending Intent")
noteBuilder.setContentText("can you pls click text so my intent get executed");

Intent intent = new Intent(this, MyTargetActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);


NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(uniqueid, noteBuilder.build());