前言:原來安卓也并沒有能夠百分百或得每臺設備固定不變的唯一 標志肴掷,
由于權限變更块蚌,刷機机蔗,重載安裝等蒲祈,將使得標識符失去唯一性甘萧,在此背景下,只能最大程度的由開發(fā)者自己根據(jù)各種IMEI讳嘱、Mac adress幔嗦、SN(設備序列號)等等酿愧,再通過不同的加密算法沥潭,生成一個極低沖突性的標識符串,然后保存在本地sd卡中嬉挡,只要用戶不刪除該卡內容(寄希望于用戶不要有什么太多非常規(guī)操作咯)钝鸽,即可獲得該設備唯一標識
import android.content.Context;
import android.os.Environment;
import android.telephony.TelephonyManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.util.UUID;
/**
* @author fangyc on 2018/08/29.
*/
public class DeviceIdUtil {
//保存文件的路徑
private static final String CACHE_DEVICES_DIR = "devices/devices";
//保存的文件 采用隱藏文件的形式進行保存
private static final String DEVICES_FILE_NAME = ".DEVICES";
/**
* 獲取設備唯一標識符
*
* @param context
* @return
*/
public static String getDeviceId(Context context) {
//讀取保存的在sd卡中的唯一標識符
String deviceId = readDeviceID(context);
//判斷是否已經生成過,
if (deviceId != null && !"".equals(deviceId)) {
return deviceId;
}
//用于生成最終的唯一標識符
StringBuffer s = new StringBuffer();
try {
//獲取IMES(也就是常說的DeviceId)
deviceId = getIMIEStatus(context);
s.append(deviceId);
} catch (Exception e) {
e.printStackTrace();
}
try {
//獲取設備的MACAddress地址 去掉中間相隔的冒號
deviceId = getLocalMac(context).replace(":", "");
s.append(deviceId);
} catch (Exception e) {
e.printStackTrace();
}
// }
//如果以上搜沒有獲取相應的則自己生成相應的UUID作為相應設備唯一標識符
if (s == null || s.length() <= 0) {
UUID uuid = UUID.randomUUID();
deviceId = uuid.toString().replace("-", "");
s.append(deviceId);
}
//為了統(tǒng)一格式對設備的唯一標識進行md5加密 最終生成32位字符串
String md5 = getMD5(s.toString(), false);
if (s.length() > 0) {
//持久化操作, 進行保存到SD卡中
saveDeviceID(md5, context);
}
return md5;
}
/**
* 讀取固定的文件中的內容,這里就是讀取sd卡中保存的設備唯一標識符
*
* @param context
* @return
*/
private static String readDeviceID(Context context) {
File file = getDevicesDir(context);
StringBuffer buffer = new StringBuffer();
try {
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
Reader in = new BufferedReader(isr);
int i;
while ((i = in.read()) > -1) {
buffer.append((char) i);
}
in.close();
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 獲取設備的DeviceId(IMES) 這里需要相應的權限<br/>
* 需要 READ_PHONE_STATE 權限
*
* @param context
* @return
*/
private static String getIMIEStatus(Context context) {
TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
return deviceId;
}
/**
* 獲取設備MAC 地址 由于 6.0 以后 WifiManager 得到的 MacAddress得到都是 相同的沒有意義的內容
* 所以采用以下方法獲取Mac地址
*
* @param context
* @return
*/
private static String getLocalMac(Context context) {
// WifiManager wifi = (WifiManager) context
// .getSystemService(Context.WIFI_SERVICE);
// WifiInfo info = wifi.getConnectionInfo();
// return info.getMacAddress();
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "";
}
return macAddress;
}
/**
* 保存 內容到 SD卡中, 這里保存的就是 設備唯一標識符
*
* @param str
* @param context
*/
private static void saveDeviceID(String str, Context context) {
File file = getDevicesDir(context);
try {
FileOutputStream fos = new FileOutputStream(file);
Writer out = new OutputStreamWriter(fos, "UTF-8");
out.write(str);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 對挺特定的 內容進行 md5 加密
*
* @param message 加密明文
* @param upperCase 加密以后的字符串是是大寫還是小寫 true 大寫 false 小寫
* @return
*/
private static String getMD5(String message, boolean upperCase) {
String md5str = "";
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] input = message.getBytes();
byte[] buff = md.digest(input);
md5str = bytesToHex(buff, upperCase);
} catch (Exception e) {
e.printStackTrace();
}
return md5str;
}
private static String bytesToHex(byte[] bytes, boolean upperCase) {
StringBuffer md5str = new StringBuffer();
int digital;
for (int i = 0; i < bytes.length; i++) {
digital = bytes[i];
if (digital < 0) {
digital += 256;
}
if (digital < 16) {
md5str.append("0");
}
md5str.append(Integer.toHexString(digital));
}
if (upperCase) {
return md5str.toString().toUpperCase();
}
return md5str.toString().toLowerCase();
}
/**
* 統(tǒng)一處理設備唯一標識 保存的文件的地址
*
* @param context
* @return
*/
private static File getDevicesDir(Context context) {
File mCropFile = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File cropdir = new File(Environment.getExternalStorageDirectory(), CACHE_DEVICES_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME);
} else {
File cropdir = new File(context.getFilesDir(), CACHE_DEVICES_DIR);
if (!cropdir.exists()) {
cropdir.mkdirs();
}
mCropFile = new File(cropdir, DEVICES_FILE_NAME);
}
return mCropFile;
}
}
感謝:
android獲取設備唯一標識完美解決方案的思考以及實現(xiàn)方式
Android 設備唯一標識(多種實現(xiàn)方案)