when we pass 0 as flag to PendingIntent as below :
PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
does it follow any flags rules means does 0 correspond to any flag by default.
If we create another PendingIntent as
PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);
will it be same as earlier and if i make any change to the data in Intent , will it now correspond to new data in Intent or still have old data.
Another problem i am facing in this is I am trying to check flag
PendingIntent.FLAG_NO_CREATE
I have written the following code :
Intent i=new Intent(this,NotifResult.class);
i.putExtra("number",50);
PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
NotificationCompat.Builder nb=new NotificationCompat.Builder(this);
nb.setSmallIcon(R.drawable.ic_launcher);
nb.setTicker("ticker is here");
nb.setWhen(System.currentTimeMillis())
.setContentTitle("just the title")
.setContentText("and the description")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pi);
Notification notif=nb.build();
NotificationManager nm=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(11, notif);
i.putExtra("number",80);
PendingIntent pisecond=PendingIntent.getActivity(this, 1, i, PendingIntent.FLAG_NO_CREATE);
if(pi.equals(pisecond))
Log.v("check","they are equal");
else
Log.v("check", "they are not equal");
notif.contentIntent=pisecond;
nm.notify(11, notif);
As per docs , PendingIntent.FLAG_NO_CREATE does not create any new object if there is an existign object. i am printing value of number in the NotifResult activity wherein number value is coming to be 80 rather than 50 expected as it should use existing intent with old value ( as per my understanding).kindly update why output is coming 80. the log is showing objects to be equal as expected.
thanks
Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. For use with getActivity(Context, int, Intent, int) , getBroadcast(Context, int, Intent, int) , and getService(Context, int, Intent, int) .
In conclusion, the general and main difference between Intent and PendingIntent is that by using the first, you want to start / launch / execute something NOW, while by using the second entity you want to execute that something in the future.
FLAG_IMMUTABLE : Indicates the Intent inside the PendingIntent cannot be modified by other apps that pass an Intent to PendingIntent.send() . An app can always use FLAG_UPDATE_CURRENT to modify its own PendingIntents. Prior to Android 12, a PendingIntent created without this flag was mutable by default.
When you call:
PendingIntent pi=PendingIntent.getActivity(this, 1, i, 0);
passing 0
as the flags parameter means that you are setting no flags.
If you call:
PendingIntent pii=PendingIntent.getActivity(this, 1, i, 0);
again, and the Intent
you pass matches the Intent
from the first call, then you will get back the same PendingIntent
as from the first call. "matches" means that the ACTION, DATA, CATEGORY and COMPONENT are all the same. Extras are not considered when matching.
If you provide different extras in the Intent
for the second call, those extras will NOT be present in the PendingIntent
when it is sent. The extras in the Intent
from the first call will be used.
I cannot explain the behaviour you are seeing regarding "50" and "80". As per the docs and as per my understanding and as per my own observations, you should see "50" and not "80". There must be something else strange going on. Are you testing on a real device? an Emulator? What version of Android?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With