Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create new Pending Intent every time in Android

How do I create a pending intent everytime? Currently my existing pending intent is getting replaced with a new one. I tried using FLAG_ONE_SHOT as well as CANCEL_CURRENT but it didn't work.

like image 322
Chintan Avatar asked Mar 08 '13 15:03

Chintan


People also ask

Why do we set pending intent in notification?

Android PendingIntent In other words, PendingIntent lets us pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

What is the difference between intent and pending intent?

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.

What is mutable pending intent?

A PendingIntent object wraps the functionality of an Intent object while allowing your app to specify something that another app should do, on your app's behalf, in response to a future action. For example, the wrapped intent might be invoked when an alarm goes off, or when the user taps on a notification.


2 Answers

add a random number to the request code like that:

Intent intent = new Intent(context, YourClassname.class);
intent.putExtra("some data", "txt");  // for extra data if needed..

Random generator = new Random();

PendingIntent i=PendingIntent.getActivity(context, generator.nextInt(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
like image 123
Zeev G Avatar answered Sep 21 '22 07:09

Zeev G


FLAG_CANCEL_CURRENT- if the described PendingIntent already exists, the current one is canceled before generating a new one.

FLAG_NO_CREATE - if the described PendingIntent does not already exist, then simply return null instead of creating it.

FLAG_ONE_SHOT - this PendingIntent can only be used once.

FLAG_UPDATE_CURRENT- if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent.

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, 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).

like image 20
Tamilselvan Kalimuthu Avatar answered Sep 21 '22 07:09

Tamilselvan Kalimuthu