获取Android设备的唯一标示符

1.UUID获取标示符方式

/**

所有的设备都可以返回一个TelephonyManager.getDeviceId()*/

所有的GSM设备(测试设备都装载有SIM卡)可以返回一个TelephonyManager.getSimSerialNumber()

所有的CDMA设备对于getSimSerialNumber()却返回一个空值!

所有添加有谷歌账户的设备可以返回一个ANDROID_ID

所有的CDMA设备对于ANDROID_ID和TelephonyManager.getDeviceId()返回相同的值(只要在设置时添加了谷歌账户)

*/

privateString getMyUUID(){

finalTelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(this.TELEPHONY_SERVICE);

finalString tmDevice, tmSerial, tmPhone, androidId;

tmDevice =""+ tm.getDeviceId();

tmSerial =""+ tm.getSimSerialNumber();

androidId =""+ android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);

UUID deviceUuid =newUUID(androidId.hashCode(), ((long)tmDevice.hashCode() <<32) | tmSerial.hashCode());

String uniqueId = deviceUuid.toString();

Log.d("debug","uuid="+uniqueId);

returnuniqueId;

}

2.Installation获取标示符方式:但是当程序卸载后再装    获取的标示符不一样,如果是在已安装的基础上更新程序,则标示符一样

public class Installation {

private static String sID = null;

private static final String INSTALLATION = "INSTALLATION";

public synchronized static String id(Context context) {

if (sID == null) {

File installation = new File(context.getFilesDir(), INSTALLATION);

try {

if (!installation.exists())

writeInstallationFile(installation);

sID = readInstallationFile(installation);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

return sID;

}

private static String readInstallationFile(File installation) throws IOException {

RandomAccessFile f = new RandomAccessFile(installation, "r");

byte[] bytes = new byte[(int) f.length()];

f.readFully(bytes);

f.close();

return new String(bytes);

}

private static void writeInstallationFile(File installation) throws IOException {

FileOutputStream out = new FileOutputStream(installation);

String id = UUID.randomUUID().toString();

out.write(id.getBytes());

out.close();

}

}

你可能感兴趣的:(获取Android设备的唯一标示符)