Android上的RFC 4122 UUID?

时间:2013-03-20 09:03:04

标签: android uuid

有没有办法在Android上生成RFC 4122版本1 UUID? UUID版本1表示基于时间戳的UUID。

3 个答案:

答案 0 :(得分:2)

您是否尝试过JUG library

在他们的示例中,他们展示了如何生成基于时间的UUID,如下面的代码:

NoArgGenerator timeBasedGenerator = Generators.timeBasedGenerator(); 
UUID firstUUID = timeBasedGenerator.generate();

或者您可以从设备网络界面生成它(如果您的Android版本允许访问硬件信息):

timeBasedGenerator = Generators.timeBasedGenerator(EthernetAddress.fromInterface());
UUID customUUID = timeBasedGenerator.generate();

答案 1 :(得分:0)

您检查过this

public static String deviceUDID(Context ctx) {
 final TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);

 final String tmDevice, tmSerial, androidId;
 tmDevice = "" + tm.getDeviceId();
 tmSerial = "" + tm.getSimSerialNumber();
 androidId = "" +android.provider.Settings.Secure.getString(ctx.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);

 UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
 String deviceId = deviceUuid.toString();
 Log.d("Device Id", deviceId);
 return deviceId;
}

您也可以参考thisthis链接。

答案 2 :(得分:0)

https://github.com/marccarre/cassandra-utils

它对我有用。 你只需要一个类:cassandra-utils / src / main / java / com / carmatech / cassandra / TimeUUID.java build.gradle中有一个依赖项:

// https://mvnrepository.com/artifact/com.eaio.uuid/uuid
**compile group: 'com.eaio.uuid', name: 'uuid', version: '3.2'**

警告将使用随机代替MAC地址,但您可以更改它。

import com.eaio.uuid.UUIDGen;

import org.joda.time.DateTime;

import java.util.Date;
import java.util.UUID;

public final class TimeUUID {
private TimeUUID() {
    // Pure utility class, do NOT instantiate.
}

private static final long NUM_100NS_INTERVALS_SINCE_UUID_EPOCH = 0x01b21dd213814000L;
private static long lastTimestamp = Long.MIN_VALUE;

/**
 * WARNING: Use only for testing purposes, as it may lead to duplicate UUIDs. Re-initialize the value of the last timestamp seen.
 */
public static synchronized void reset() {
    lastTimestamp = Long.MIN_VALUE;
}

/**
 * Generate a new, unique UUID based on current timestamp.
 */
public static UUID createUUID() {
    return createUUID(System.currentTimeMillis());
}

/**
 * Generate a new, unique UUID based on the provided date-time.
 *
 * @param dateTime date-time used for the "time" component of the UUID.
 */
public static UUID createUUID(final DateTime dateTime) {
    return createUUID(dateTime.getMillis());
}

/**
 * Generate a new, unique UUID based on the provided date.
 *
 * @param javaDate date used for the "time" component of the UUID.
 */
public static UUID createUUID(final Date javaDate) {
    return createUUID(javaDate.getTime());
}

/**
 * Generate a new, unique UUID based on the provided timestamp.
 *
 * @param timestamp timestamp used for the "time" component of the UUID.
 */
public static UUID createUUID(final long timestamp) {
    final long timestampIn100Ns = to100Ns(timestamp);
    final long uniqueTimestampIn100Ns = makeUnique(timestampIn100Ns);
    return new UUID(toUUIDTime(uniqueTimestampIn100Ns), UUIDGen.getClockSeqAndNode());
}

private static long to100Ns(final long timestampInMs) {
    return (timestampInMs * 10000) + NUM_100NS_INTERVALS_SINCE_UUID_EPOCH;
}

private static synchronized long makeUnique(final long timestamp) {
    if (timestamp > lastTimestamp) {
        lastTimestamp = timestamp;
        return timestamp;
    } else {
        return ++lastTimestamp;
    }
}

private static long toUUIDTime(final long timestampIn100Ns) {
    // Example:
    // Lowest 16 bits and version 1: 0123 4567 89AB CDEF -> 89AB CDEF 0000 0000 -> 89AB CDEF 0000 1000
    // Middle 32 bits: 0123 4567 89AB CDEF -> 0000 4567 0000 0000 -> 0000 0000 4567 0000 -> 89AB CDEF 4567 1000
    // Highest 16 bits: 0123 4567 89AB CDEF -> 0123 0000 0000 0000 -> 0000 0000 0000 0123 -> 89AB CDEF 4567 1123

    long uuidTime = (timestampIn100Ns << 32) | 0x0000000000001000L;
    uuidTime |= (timestampIn100Ns & 0x0000FFFF00000000L) >>> 16;
    uuidTime |= (timestampIn100Ns & 0xFFFF000000000000L) >>> 48;
    return uuidTime;
}

/**
 * WARNING: returned UUID is not unique. Get the UUID corresponding to the provided date-time and the clock sequence, which depends on the IP and MAC
 * addresses of the current machine, and a random component per process/JVM.
 *
 * @param dateTime date-time used for the "time" component of the UUID.
 */
public static UUID toUUID(final DateTime dateTime) {
    return toUUID(dateTime.getMillis());
}

/**
 * WARNING: returned UUID is not unique. Get the UUID corresponding to the provided date and the clock sequence, which depends on the IP and MAC addresses
 * of the current machine, and a random component per process/JVM.
 *
 * @param javaDate date used for the "time" component of the UUID.
 */
public static UUID toUUID(final Date javaDate) {
    return toUUID(javaDate.getTime());
}

/**
 * WARNING: returned UUID is not unique. Get the UUID corresponding to the provided timestamp and the clock sequence, which depends on the IP and MAC
 * addresses of the current machine, and a random component per process/JVM.
 *
 * @param timestamp timestamp used for the "time" component of the UUID.
 */
public static UUID toUUID(final long timestamp) {
    final long timestampIn100Ns = to100Ns(timestamp);
    return new UUID(toUUIDTime(timestampIn100Ns), UUIDGen.getClockSeqAndNode());
}

/**
 * Extract the "time" component of the provided UUID.
 *
 * @param uuid UUID to extract timestamp from.
 * @return Timestamp in milliseconds.
 */
public static long toMillis(final UUID uuid) {
    return from100Ns(uuid.timestamp());
}

private static long from100Ns(long timestampIn100Ns) {
    return (timestampIn100Ns - NUM_100NS_INTERVALS_SINCE_UUID_EPOCH) / 10000;
}

}