Create new Pending Intent every time in Android

2019-01-26 04:38发布

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.

2条回答
戒情不戒烟
2楼-- · 2019-01-26 05:25

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

查看更多
欢心
3楼-- · 2019-01-26 05:26

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);
查看更多
登录 后发表回答