2013-03-20 40 views

回答

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聯繫過。

+0

這將在平板電腦上工作(我不確定TelephonyManager是否可用) – 2014-10-17 17:27:31

+0

這一代的時間戳在哪裏? – Navigateur 2015-06-04 14:05:09

+0

@Navigateur:在這裏檢查http://developer.android.com/reference/java/util/UUID.html#timestamp() – 2015-06-04 14:49:33

0

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

這對我的作品。 ü只需要一個類:卡桑德拉-utils的/ src目錄/主/ JAVA/COM/carmatech /卡桑德拉/ 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:AB CDEF -> 89AB CDEF 0000 0000 -> 89AB CDEF 0000 1000 
    // Middle 32 bits:AB CDEF -> 0000 4567 0000 0000 -> 0000 0000 4567 0000 -> 89AB CDEF 4567 1000 
    // Highest 16 bits:AB CDEF ->-> 0000 0000 0000-> 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; 
} 

}

2

你試過JUG library

在他們的榜樣,他們展示如何生成一個基於時間的UUID像下面的代碼:

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

或者你也可以從設備網絡接口生成它(如果您的Android版本可以讓硬件信息的訪問):

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