Create new Pending Intent every time in Android

前端 未结 2 2095
梦如初夏
梦如初夏 2020-12-18 20:27

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

相关标签:
2条回答
  • 2020-12-18 20:51

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

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题