赞
踩
android 11 以前使用的MAC的设备唯一码的方式被废弃了,因为11不让用了,然后就去官方上找推荐了;
之前的获取MAC的地址,有兴趣的可以点进去看看
官方推荐使用UUID ;这个好像目前几个大厂的APP都在使用的方法
具体的就直接上代码吧;
下面的代码直接获取UUID
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
}
这里的调用每次都会去创建一个UUID;这样我们就不能保持唯一性了,因为每次都会创建新的UUID,所以我们就需要把这个UUID保存到我们的内存当中去;这样每次可以检测,如果没有这个保存的这个文件那么就会创建新的UUID,保存到我们的文件中;如果不卸载的话,或者被人为的手动删除这个文件的话,那么UUID是不会改变的,那么我们的唯一性是可以得到保证的,我们也需要提前获取我们的存储权限,否则无法进行读取的
public static void saveBitmap() throws IOException { // 创建目录 //获取内部存储状态 String state = Environment.getExternalStorageState(); //如果状态不是mounted,无法读写 if (!state.equals(Environment.MEDIA_MOUNTED)) { return; } String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath(); File appDir = new File(sdCardDir, "CaChe"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "xxxxx" + ".txt";//这里是创建一个TXT文件保存我们的UUID File file = new File(appDir, fileName); if (!file.exists()) { file.createNewFile(); } //保存android唯一表示符 try { FileWriter fw = new FileWriter(file); fw.write(getUUID()); fw.flush(); fw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
文件创建好了,我们进行获取这个文件
public static String readKey() throws IOException { // 创建目录 //获取内部存储状态 String state = Environment.getExternalStorageState(); //如果状态不是mounted,无法读写 if (!state.equals(Environment.MEDIA_MOUNTED)) { return null; } String sdCardDir = Environment.getExternalStorageDirectory().getAbsolutePath(); File appDir = new File(sdCardDir, "CaChe"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "xxxxx" + ".txt";//这里是进行读取我们保存文件的名称 File file = new File(appDir, fileName); if (!file.exists()) { file.createNewFile(); } BufferedReader reader = null; StringBuilder content=null; try { FileReader fr = new FileReader(file); content= new StringBuilder(); reader = new BufferedReader(fr); String line; while ((line= reader.readLine())!=null){ content.append(line); } } catch (Exception e) { e.printStackTrace(); }finally { if (reader!=null){ try { reader.close(); }catch (IOException e){ e.printStackTrace(); } } } return content.toString(); }
这样我们就完成了一个唯一标识符的创建
之后的调用 我们只需要调用我们的 readKey() 方法就可以进行获取了
String imei = PhoneInfoUtil.readKey(); //这里是一个例子 返回值就是唯一标识符
以上,有问题欢迎提问,共同进步
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。