2

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

4

3 回答 3

2

你试过JUG 库吗?

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

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

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

timeBasedGenerator = Generators.timeBasedGenerator(EthernetAddress.fromInterface());
UUID customUUID = timeBasedGenerator.generate();
于 2018-02-17T18:44:10.607 回答
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;
}

}

于 2017-06-29T08:34:07.943 回答
-1

你检查过这个吗

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;
}

您也可以参考这个这个链接。

于 2013-03-20T09:19:33.133 回答