android : 更新android 11 后 获取设备唯一码的方式 |
您所在的位置:网站首页 › 手机的识别码怎么改 › android : 更新android 11 后 获取设备唯一码的方式 |
android 11 发布了
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 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |