What's requestCode used for on PendingIntent?
requestCode
is used to retrieve the same pending intent instance later on (for cancelling, etc).- Yes, my guess is the alarms will override each other. I would keep the request codes unique.
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.
PendingIntent get requestCode
You need to put lecture.getId()
into extras of your myIntent
. According to Javadoc requestCode is not even used yet.
// store id
myIntent.putExtra("id", lecture.getId());
// read id or -1, if there is no such extra in intent
int id = myIntent.getIntExtra("id", -1);
Has requestCode in PendingIntent always been supported?
Yes. The requestCode
has always been there. It isn't currently used by the Android framework to do anything other than as part of the test for PendingIntent
matching. Using requestCode
to determine different PendingIntent
s is robust and supported. The documentation even says so:
- 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 theIntent
attributes considered byIntent.filterEquals
, or different request code integers supplied togetActivity(Context, int, Intent, int)
,getActivities(Context, int, Intent[], int)
,getBroadcast(Context, int, Intent, int)
, orgetService(Context, int, Intent, int)
.
Keep both pendingIntents with the same requestCode
I am asking if there exists a certain type of flag that will be able to differentiate them
There is not, and it makes sense because this is already the purpose of the requestCode parameter.
For information, these are your options regarding flags:
You will have to change your mechanism to make it possible to have different requestCodes for Pending Intents. It may be a lot of work, but it is what you have to do.
How i can get requestcode from pending intent for showing which message delivered
You should add your SMS id
to the PendingIntent
like this:
Intent deliveryIntent = new Intent(DELIVERED);
deliveryIntent.putExtra("id", id);
deliveredPI = PendingIntent.getBroadcast(this, id, deliveryIntent, 0);
Then, in onReceive()
you can get the SMS id like this:
int id = intent.getIntExtra("id", -1);
if (id >= 0) {
// Got a valid id...
}
How to create unique pendingIntents without requestCode
this question maybe a duplicate of this.
Your requestCode must be unique otherwise PendingIntent will be updated to last intent if you use the same requestCode(old Api <22).
If your are not using action, you can add your UUID as de dummy action to make them unique.
You have to store the requestCode to be able to cancel old PendingIntent.
How to get requestCode from pending intent at the time of alarm in android
You can put requestCode
as extra to your intent. Like following:
intent.putExtra("requestCode", requestCode);
Then you can get it in Activity class by:
int requestCode = received_intent.getExtras().getInt("requestCode");
Cancelling a PendingIntent with unique requestCode
You are trying to create a service with your PendingIntent and are trying to cancel a Broadcast. It will never work.
You need to getService with the PendingIntent you are trying to cancel.
Related Topics
Couldn't Get Connection Factory Client - Fighting with Google Maps
Android in Navigation Drawer When I Click Anywhere Drawer Closes
Parsing Date/Time to Localtimezone
Firebase Servervalue.Timestamp in Java Data Models Objects
Android 4.3 and Phonegap, Cant Tap Link
Borders on Certain Zoom Levels on Webpage
How to Add Network_Security_Config.Xml to Manifest in Expo App Without Ejecting
Call Recorder Not Working in Android 10 (Q)
Android Listview Child View Setenabled() and Setclickable() Do Nothing
How to Compile Ffmpeg-2.2.2 on Windows with Cygwin and Android Ndk R9C
Fragment's Onsaveinstancestate() Is Never Called
Gradle Dsl Method Not Found: 'Implementation()'
Android Clickablespan Not Calling Onclick
Air 3 Native Extensions for Android - Can I/How to Include 3Rd Party Libraries
Android Listview with Header and Footer Buttons
Add Maven Repository to Build.Gradle
Using Numberpicker Widget with Strings
Android Browser Textarea Scrolls All Over The Place, Is Unusable