Pending Intents in Android

The PendingIntent class provides a mechanism for creating Intents that can be fi red on your application’s behalf by another application at a later time

A Pending Intent is commonly used to package Intents that will be fi red in response to a future event, such as a Widget or Notification being clicked.

The PendingIntent class offers static methods to construct Pending Intents used to start an Activity, to start a Service, or to broadcast an Intent:

int requestCode = 0;
int flags = 0;
// Start an Activity
Intent startActivityIntent = new Intent(this, MyOtherActivity.class);
PendingIntent.getActivity(this, requestCode, 
 startActivityIntent, flags);
// Start a Service
Intent startServiceIntent = new Intent(this, MyService.class);
PendingIntent.getService(this, requestCode, 
 startServiceIntent , flags);
// Broadcast an Intent
Intent broadcastIntent = new Intent(NEW_LIFEFORM_DETECTED);
PendingIntent.getBroadcast(this, requestCode, 
 broadcastIntent, flags);

The PendingIntent class includes static constants that can be used to specify flags to update or cancel any existing Pending Intent that matches your specified action, as well as to specify if this Intent is to be fired only once.

Leave a Comment