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();    //这里是一个例子    返回值就是唯一标识符

以上,有问题欢迎提问,共同进步

  • 3
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值