有沒有辦法在Android上生成RFC 4122版本1的UUID? UUID版本1表示基於時間戳的UUID。Android上的RFC 4122 UUID?
2
A
回答
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;
}
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();
相關問題
- 1. 的RFC 4122第4版
- 2. 爲RFC 4122 Version 1 UUID格式化MAC地址節點的正確方法是什麼?
- 3. CouchDB中的UUID
- 4. Java中爲空(「nil」)的UUID的實例
- 5. SAP中的Android調用RFC
- 6. 獲取UUID的Android
- 7. 在android(UUID)
- 8. Android和Java Uuid
- 9. iOS上的基於時間的UUID(版本1)?
- 10. 設備UUID上
- 11. 轉換到Android UUID
- 12. 通過網絡傳輸UUID和字節
- 13. 根據RFC RFC Flex 2396
- 14. Android的藍牙UUID連接APP到ANDROID
- 15. Android檢查有效UUID
- 16. UUID藍牙和Android設備
- 17. 如何更改ANDROID Emulator UUID?
- 18. 在Android 2.0中查找UUID
- 19. Android藍牙設備UUID
- 20. Android藍牙LE掃描UUID
- 21. 休眠的UUID UUID類型
- 22. 爲什麼UUID標準很複雜,我應該遵循它們嗎?
- 23. RFC 6314
- 24. Android是否支持RFC 2821 SMTP標準?
- 25. .Net Uri編碼RFC 2396 vs RFC 3986
- 26. 休眠UUID代和MySQL UUID功能UUID()
- 27. 在Laravel上選擇Postgres UUID
- 28. Android - 獲取此設備的藍牙UUID
- 29. Android:使用UUID作爲SQLite的主鍵
- 30. Android:獲取uuid的藍牙設備
這將在平板電腦上工作(我不確定TelephonyManager是否可用) – 2014-10-17 17:27:31
這一代的時間戳在哪裏? – Navigateur 2015-06-04 14:05:09
@Navigateur:在這裏檢查http://developer.android.com/reference/java/util/UUID.html#timestamp() – 2015-06-04 14:49:33