PendingIntent is a powerful mechanism in the Android SDK that allows applications to perform actions on behalf of another application. It provides a token that you can give to another application (e.g., NotificationManager, AlarmManager, etc.), which allows that application to execute your code at a later time, even if your application is no longer running.
Essentially, a PendingIntent encapsulates an Intent and provides permission for the wrapped Intent to be executed by another application. It is particularly useful in scenarios where you want an external component, such as a system service or another application, to trigger your application’s code.
When a PendingIntent is created, it stores the original Intent and the security permissions necessary to execute it. Internally, the Android framework manages the lifecycle of the PendingIntent, allowing it to be invoked at a later time. The structure of the PendingIntent includes the target component (an Activity, Service, or BroadcastReceiver) and the parameters needed to carry out the action specified in the Intent.
This example demonstrates how to create a simple notification with a PendingIntent that launches an Activity when clicked:
// Create an intent to launch the activity
Intent intent = new Intent(this, YourActivity.class);
// Create the PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
// Build the notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Example Notification")
.setContentText("Click to open your activity!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent) // Attach the PendingIntent to the notification
.setAutoCancel(true); // Dismiss notification on click
// Issue the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?