获取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();

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 链接地址:http://www.cnblogs.com/lvcha/p/3721091.html DEVICE_I...
    Tony1213阅读 28,427评论 3 15
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,826评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • 感恩一天气越来越热,感恩各行各业坚守在工作岗位上的人们,有你们的付出才有我们舒适的工作和生活环境。特别感恩农民朋友...
    毛毛细雨mmxy阅读 128评论 0 0
  • 天好心情好 今天终于不下雨了,看到外面晴朗的天空,深深地呼吸一下,觉得心情也舒展开来。真的是那句话“天放晴心就晴”...
    麦苗儿阅读 344评论 0 0