1.许多应用都有这样的需求获取你的手机唯一标识,当你卸载软件重新安装的时候也要保证这个唯一标识不变,所以只能根据手机硬件的去获取到唯一标识.
2.有好多跟硬件相关都会有各种权限问题。一种很简单直接获取到READ_PHONE_STATE的权限,读取到设备串号.
3.如果是Android6.0系统需要去申请权限,否则会奔溃,这是6.0的新特性。虽然没有什么暖用。
(1)这种方式不加密,直接获取串号
if (EasyPermissions.hasPermissions(context, Manifest.permission.READ_PHONE_STATE)) {
// 已经有摄像头权限了,可以使用该权限完成app的相应的操作了
Log.e(“执行请求”, “获取true”);
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
deviceId = telephonyManager.getDeviceId();// imei
if (deviceId == null) {
deviceId = telephonyManager.getSubscriberId();// imsi
}
if (deviceId == null) {
deviceId = telephonyManager.getSimSerialNumber();
}
if (deviceId == null) {
deviceId = “noimei” + telephonyManager.getNetworkOperatorName();
}
Log.e("手机唯一标志deviceId", deviceId);
// textView.setText(“设备号”+deviceId);
return deviceId;
} else {
EasyPermissions.requestPermissions(context, "请您为“软件”不打开读取手机标志权限",
2, Manifest.permission.READ_PHONE_STATE);
return null;
}
(2) 这种方式为多种硬件相关组合到一块,加密方式
//获取手机唯一串号
String szImei = null;
if (EasyPermissions.hasPermissions(context, Manifest.permission.READ_PHONE_STATE)) {
TelephonyManager TelephonyMgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
szImei = TelephonyMgr.getDeviceId();
} else {
// EasyPermissions.requestPermissions(context, “请您为打开唯一标志权限”,
// 2, Manifest.permission.READ_PHONE_STATE);
}
String m_szDevIDShort = "35" + //we make this look like a valid IMEI
Build.BOARD.length() % 10 +
Build.BRAND.length() % 10 +
Build.CPU_ABI.length() % 10 +
Build.DEVICE.length() % 10 +
Build.DISPLAY.length() % 10 +
Build.HOST.length() % 10 +
Build.ID.length() % 10 +
Build.MANUFACTURER.length() % 10 +
Build.MODEL.length() % 10 +
Build.PRODUCT.length() % 10 +
Build.TAGS.length() % 10 +
Build.TYPE.length() % 10 +
Build.USER.length() % 10; //13 digits
String m_szAndroidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
String m_szWLANMAC = wm.getConnectionInfo().getMacAddress();
String m_szLongID = szImei + m_szDevIDShort
+ m_szAndroidID + m_szWLANMAC;
// compute md5
MessageDigest m = null;
try {
m = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
m.update(m_szLongID.getBytes(), 0, m_szLongID.length());
// get md5 bytes
byte p_md5Data[] = m.digest();
// create a hex string
String m_szUniqueID = new String();
for (int i = 0; i < p_md5Data.length; i++) {
int b = (0xFF & p_md5Data[i]);
// if it is a single digit, make sure it have 0 in front (proper padding)
if (b <= 0xF)
m_szUniqueID += "0";
// add number to string
m_szUniqueID += Integer.toHexString(b);
} // hex string to uppercase
m_szUniqueID = m_szUniqueID.toUpperCase();
Log.e("手机唯一标志deviceId", m_szUniqueID);
- 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
这个是我的demo下载地址,欢迎体验
http://download.csdn.net/detail/u014007519/9629539