You can use the generateId methods in the LocalKeyGenerator and KeyGenerator classes to generate an ID when creating a new object for which you require a primary key.
This method in the LocalKeyGenerator class generates a unique ID for the package on the local device:
public static long generateId()
This method in the KeyGenerator class generates a unique ID for the same package across all devices:
public static long generateId()