What is an Android PendingIntent?
A PendingIntent
is a token that you give to a foreign application (e.g. NotificationManager
, AlarmManager
, Home Screen AppWidgetManager
, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
If you give the foreign application an Intent, it will execute your Intent
with its own permissions. But if you give the foreign application a PendingIntent
, that application will execute your Intent
using your application's permission.
Differences between Intent and PendingIntent
Intent
An Android Intent is an object carrying an intent, i.e. a message from one component to another component either inside or outside of the application. Intents can communicate messages among any of the three core components of an application -- Activities, Services, and BroadcastReceivers.
The intent itself, an Intent object, is a passive data structure. It holds an abstract description of an operation to be performed.
For example: say you have an Activity that needs to launch an email client and send an email. To do this, your Activity would send an Intent with the action ACTION_SEND
, along with the appropriate chooser, to the Android Intent Resolver:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
The specified chooser gives the proper interface for the user to pick how to send your email data.
EXPLICIT INTENTS
// Explicit Intent by specifying its class name
Intent i = new Intent(this, TargetActivity.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts TargetActivity
startActivity(i);
IMPLICIT INTENTS
// Implicit Intent by specifying a URI
Intent i = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.example.com"));
// Starts Implicit Activity
startActivity(i);
Pending Intent
A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
By giving a PendingIntent to another application, you are granting it
the right to perform the operation you have specified as if the other
application was yourself (with the same permissions and identity). As
such, you should be careful about how you build the PendingIntent:
almost always, for example, the base Intent you supply should have the
component name explicitly set to one of your own components, to ensure
it is ultimately sent there and nowhere else.
Example for Pending Intent : http://android-pending-intent.blogspot.in/
Source : Android Intents and Android Pending Intents
Hope this helps.
Android - What is a PendingIntent ?
Q1 - In what way is it "Pending"?
The system stores the values you store in PendingIntent
and lets you (or another part of the framework) look them up later on, as if the component that looked them up had created a new Intent spontaneously with that information.
Q2 - how does "reference to a token" relate to my code here?
The Android Framework doesn't actually store the PendingIntent object you create; it hashes the "identifying information" for the intent (in this case, the action, data, type, class, and categories) and uses that to look up the rest of the information. The literal PendingIntent
object you create doesn't get saved, the information it represents does.
Q3 - What are the "extra contents"?
The "extras" it's referring to here are the parcelable items you store via putExtra()
. The requestCode and flags values are also saved and retrieved, but when the documentation refers to "extras" it means the literal getExtras()
Bundle that Intents can use to carry additional information.
Why do we use PendingIntent in notifications?
I was wondering why we use pendingIntent while creating notifications.
Read the PendingIntent documentation. Specifically this bit:
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
In other words - you use a PendingIntent for a notification because a notification can sit in the user's notification area for an indefinite amount of time, during which your app may have been killed.
However, I can't wrap my head around how a PendingIntent creates a fresh task and back stack.
It doesn't. You do, as explained in the very article you linked.
Does a Pending Intent have a lifetime?
PendingIntent
s do not expire. However, they are not persistent and will not survive a reboot of the device.
To see if the PendingIntent
still exists, you can use the following adb command:
adb shell dumpsys activity intents
This lists all of the PendingIntent
s in the system. You can also look at the notifications using:
adb shell dumpsys notification
This will show all Notification
s, including the PendingIntent
s that are set for them.
Explain the difference between Intent and PendingIntent
Pending intent
is intent which will start later on. Normal intent
is start at the time when passed to startActivity(intent) or StartService(intent)
A PendingIntent is a token that you give to another application (e.g. Notification Manager, Alarm Manager or other 3rd party applications), which allows this other application to use the permissions of your application to execute a predefined piece of code.
To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent.getBroadcast().
To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
What happens to a PendingIntent if the target app was force-closed?
The answer is short: Active PendingIntents are cancelled on an application force-stop.
What is the request code in PendingIntent in android studio
The integer parameter requestCode
is a way to differentiate between multiple intents that have the same action, data, type, identity, class, and categories. Otherwise if you create a second intent with those same properties, even if the extras are different, it will not work.
From the documentation
A PendingIntent itself is simply a reference to a token maintained by
the system describing the original data used to retrieve it. This
means that, even if its owning application's process is killed, the
PendingIntent itself will remain usable from other processes that have
been given it. If the creating application later re-retrieves the same
kind of PendingIntent (same operation, same Intent action, data,
categories, and components, and same flags), it will receive a
PendingIntent representing the same token if that is still valid, and
can thus call cancel() to remove it.Because of this behavior, it is important to know when two Intents are
considered to be the same for purposes of retrieving a PendingIntent.
A common mistake people make is to create multiple PendingIntent
objects with Intents that only vary in their "extra" contents,
expecting to get a different PendingIntent each time. This does not
happen. The parts of the Intent that are used for matching are the
same ones defined by Intent#filterEquals(Intent). If you use two
Intent objects that are equivalent as per Intent#filterEquals(Intent),
then you will get the same PendingIntent for both of them.There are two typical ways to deal with this.
If you truly need multiple distinct PendingIntent objects active at
the same time (such as to use as two notifications that are both shown
at the same time), then you will need to ensure there is something
that is different about them to associate them with different
PendingIntents. This may be any of the Intent attributes considered by
Intent#filterEquals(Intent), or different request code integers
supplied to getActivity(Context, int, Intent, int),
getActivities(Context, int, Intent[], int), getBroadcast(Context, int,
Intent, int), or getService(Context, int, Intent, int).
You are right by the way, the documentation on the specific parameter, Private request code for the sender is not very helpful in my opinion.
Android 12 Pending Intent
You need to do this:
pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
action), PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_IMMUTABLE);
Since you are using AlarmManager
you should be able to use the IMMUTABLE
flag.
Related Topics
Tabwidget Current Tab Bottom Line Color
Android Activity Over Default Lock Screen
How to Dynamically Set the Position of View in Android
Asynctask Won't Stop Even When the Activity Has Destroyed
How to Implement Recyclerview with Cardview Rows in a Fragment with Tablayout
How to Programmatically Open the Permission Screen for a Specific App on Android 6.0 (Marshmallow)
Spinner: Get State or Get Notified When Opens
Call Activity Method from Adapter
Google Sign in Not Working After Publishing in Play Store
Dynamically Changing the Fragments Inside a Fragment Tab Host
How to Build an Executable for Android Shell
Android Sdk Manager Won't Open
Calling Activity Class Method from Service Class
Android Paint: .Measuretext() VS .Gettextbounds()