deviceid_关于android设备唯一区分device id的取得

电脑杂谈  发布时间:2016-05-14 08:05:47  来源:网络整理


有些apk为了区分唯一设备,需要用到一个device id。
1. 取得设备的MAC address
2. 使用TelephonyManager的getdeviceid()
3. 另外还有一个android系统的唯一区分ANDROID_ID,
Settings.Secure#ANDROID_ID returns the Android ID as an unique 64-bit hex string.

import android.provider.Settings.Secure;
private String android_id = Secure.getString(getContext().getContentResolver(),


// question
Do Android devices have a unique id, and if so, what is a simple way to access it via java?
// answer 1
There are many answers to this question, most of which will only work "some" of the time, and unfortunately that's not good enough.

Based on my tests of devices (all phones, at least one of which is not activated):

* All devices tested returned a value for TelephonyManager.getdeviceid()
* All GSM devices (all tested with a SIM) returned a value for TelephonyManager.getSimSerialNumber()
* All CDMA devices returned null for getSimSerialNumber() (as expected)
* All devices with a Google account added returned a value for ANDROID_ID
* All CDMA devices returned the same value (or derivation of the same value) for both ANDROID_ID and TelephonyManager.getdeviceid() -- as long as a Google account has been added during setup.
* I did not yet have a chance to test GSM devices with no SIM, a GSM device with no Google account added, or any of the devices in airplane mode.

So if you want something unique to the device itself, TM.getdeviceid() should be sufficient. Obviously some users are more paranoid than others, so it might be useful to hash 1 or more of these identifiers, so that the string is still virtually unique to the device, but does not explicitly identify the user's actual device. For example, using String.hashCode(), combined with a UUID:

final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); final String tmDevice, tmSerial, androidId; tmDevice = "" tm.getDeviceId(); tmSerial = "" tm.getSimSerialNumber(); androidId = "" android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID); UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode()); String deviceid = deviceUuid.toString();

might result in something like: 00000000-54b3-e7c7-0000-000046bffd97

It works well enough for me.

As Richard mentions below, don't forget that you need permission to read the TelephonyManager properties, so add this to your manifest:



    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论