DeviceUuidFactory
来源:互联网 发布:配电网优化运行 编辑:程序博客网 时间:2024/05/01 19:55
安卓设备唯一码工具类
[1].[代码] [Java]代码 跳至 [1]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import
android.content.Context;
import
android.content.SharedPreferences;
import
android.provider.Settings.Secure;
import
android.telephony.TelephonyManager;
import
java.io.UnsupportedEncodingException;
import
java.util.UUID;
public
class
DeviceUuidFactory {
protected
static
final
String PREFS_FILE =
"device_id.xml"
;
protected
static
final
String PREFS_DEVICE_ID =
"device_id"
;
protected
static
UUID uuid;
public
DeviceUuidFactory(Context context) {
if
( uuid ==
null
) {
synchronized
(DeviceUuidFactory.
class
) {
if
( uuid ==
null
) {
final
SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE,
0
);
final
String id = prefs.getString(PREFS_DEVICE_ID,
null
);
if
(id !=
null
) {
// Use the ids previously computed and stored in the prefs file
uuid = UUID.fromString(id);
}
else
{
final
String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
// Use the Android ID unless it's broken, in which case fallback on deviceId,
// unless it's not available, then fallback on a random number which we store
// to a prefs file
try
{
if
(!
"9774d56d682e549c"
.equals(androidId)) {
uuid = UUID.nameUUIDFromBytes(androidId.getBytes(
"utf8"
));
}
else
{
final
String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
uuid = deviceId!=
null
? UUID.nameUUIDFromBytes(deviceId.getBytes(
"utf8"
)) : UUID.randomUUID();
}
}
catch
(UnsupportedEncodingException e) {
throw
new
RuntimeException(e);
}
// Write the value out to the prefs file
prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();
}
}
}
}
}
/**
* Returns a unique UUID for the current android device. As with all UUIDs, this unique ID is "very highly likely"
* to be unique across all Android devices. Much more so than ANDROID_ID is.
*
* The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
* TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back
* on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
* usable value.
*
* In some rare circumstances, this ID may change. In particular, if the device is factory reset a new device ID
* may be generated. In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2
* to a newer, non-buggy version of Android, the device ID may change. Or, if a user uninstalls your app on
* a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.
*
* Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT
* change after a factory reset. Something to be aware of.
*
* Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
*
* @see http://code.google.com/p/android/issues/detail?id=10603
*
* @return a UUID that may be used to uniquely identify your device for most purposes.
*/
public
UUID getDeviceUuid() {
return
uuid;
}
}
0 0
- DeviceUuidFactory
- DeviceUuidFactory
- Java语言基础
- assert.h使用介绍
- Java调用Domino LDAP
- 图片文字的控件ImageTextControl
- C语言编译错误 fatal error C1083: Cannot open include file: 'stdio.h': No such file or directory
- DeviceUuidFactory
- Matrix Factorization, Algorithms, Applications, and Avaliable packages
- 利用OpenCV + PyQt4操作摄像头的demo
- 微信NATIVE动态支付(V3版本)
- Problem - 501B - Codeforces(string+map)
- gnuplot 拟合 fit函数的用法
- 6、文件的路径问题、错误的处理
- 仿函数
- 黑马程序员——Mac OS X终端的常用操作命令(UNIX指令)