0

I am creating an Android application that sets alarms based on the String date of a list of Objects (PendingIntents, with each taking in an id, that is an integer). In order to cancel the PendingIntent, I must know the PendingIntent's id.

I am having trouble of figuring out a way to generate a unique id reliably with a minimal chance of collisions. If I simply convert the String date to miliseconds, I believe that would not be reliable since the user can change timezones on their phone and the computer can make timestamp computations that are off by a few miliseconds.

5
  • 2
    Define "minimal". A random 32-bit integer has a chance of collision so low that you're virtually guaranteed to never have that problem as long as your app lives - is that not enough for you? Mar 18, 2021 at 7:17
  • @KilianFoth Gotcha. Should I use Java's hashCode function to convert the String into an integer or use the timestamp in miliseconds as the id?
    – Adam Lee
    Mar 18, 2021 at 7:26
  • 2
    One option could be the randomUUID method - stackoverflow.com/questions/2982748/create-a-guid-in-java/… Mar 18, 2021 at 23:15
  • timestamp in seconds.
    – Eyosiyas
    Mar 24, 2021 at 15:26
  • You can use uptimeMillis() or one of the other monotonic clocks. They won't go backwards except on reboot, and I'm pretty sure PendingIntents don't survive reboot.
    – Jerry101
    Mar 27, 2021 at 3:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.