分享

【技术】获取设备唯一标识

 爱开发 2022-08-02 发布于广东

如果你想第一时间收到类似的文章

点击上面↑「爱开发」关注我们!

今天分享点比较基础的东西,别小看这基础的东西,又快到了跳槽旺季,一个小小的知识点,有时能决定你是否被录用。笔者两年前就因为一个基础错过了一个Offer,深感遗憾。声明,本文讲的知识点过于基础,高手请直接关闭页面。

今天分享的是获取Android设备唯一标识,希望对你有帮助。

1、使用IMEI

    public synchronized static String getDeviceId(Context mContext) {

        TelephonyManager telephonyManager = (TelephonyManager)mContext.getSystemService(mContext.TELEPHONY_SERVICE);

        String deviceId= telephonyManager.getDeviceId();

        return deviceId;

    }

需要在AndroidManifest.xml加上权限

  <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

注意,此方法只对手机有效,对于对讲机那类并不起效,获取不到。

2、MAC Address 使用网卡地址

public synchronized static String getMacAddress(Context mContext) {

        WifiManager wm = (WifiManager)mContext.getSystemService(mContext.WIFI_SERVICE);

        String macAddress = wm.getConnectionInfo().getMacAddress();

        return macAddress ;

    }

需要在AndroidManifest.xml加上权限

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

3、使用蓝牙地址

public synchronized static String getMacId(Context mContext) {

        BluetoothAdapter bluetoothAdapter= BluetoothAdapter.getDefaultAdapter();

        String mBluethId= bluetoothAdapter.getAddress();

        return mBluethId;

    }

需要在AndroidManifest.xml加上权限

    <uses-permission android:name="android.permission.BLUETOOTH"/>

3、安装ID (Installtion ID)

作为一枚Android开发者,我们都知道Android设备的多样性,考虑到平板没有通话功能,低端设备没有WLAN或者蓝牙功能,亦或是有些用户禁止了应用程序这些需要的权限。通过上面的方法我们就获取不到唯一标识了。下面我们使用Installtion ID,无需权限的方法;这种方式的原理是在程序安装后第一次运行时生成一个ID,该方式和设备唯一标识不一样,不同的应用程序会产生不同的ID,同一个程序重新安装ID也会不同。这不是设备的唯一ID,但是可以保证每个用户的ID是不同的。可以说是用来标识每一份应用程序的唯一ID(Installtion ID),可以用来跟踪应用的安装数量等。

public synchronized static String getUUID(Context mContext) {

        if (sID == null) {

            File installation = new File(mContext.getFilesDir(), INSTALLATION);

            try {

                if (!installation.exists()) {

                    writeInstallationFile(installation);

                }

                sID = readInstallationFile(installation);

            } catch (Exception e) {

                throw new RuntimeException(e);

            }

        }

        return sID;

    }

    private static String readInstallationFile(File installation) throws IOException {

        RandomAccessFile f = new RandomAccessFile(installation, "r");

        byte[] bytes = new byte[(int) f.length()];

        f.readFully(bytes);

        f.close();

        return new String(bytes);

    }

    private static void writeInstallationFile(File installation) throws IOException {

        FileOutputStream out = new FileOutputStream(installation);

        String id = UUID.randomUUID().toString();

        out.write(id.getBytes());

        out.close();

    }

我们也可以考虑把上面获取的几个方法拼接后,计算出的MD5值来产生一个结果,也可以作为唯一的标识用。例如这样:

   public static String  getLongId(){

        String longId ="上面几个方法合并";

        MessageDigest messageDigest = null;

        try {

            messageDigest = MessageDigest.getInstance("MD5");

        } catch (NoSuchAlgorithmException e) {

            e.printStackTrace();

        }

        messageDigest.update(longId.getBytes(),0,longId.length());

        byte md5Data[] = messageDigest.digest();

        String uniqueId = new String();

        for (int i=0;i<md5Data.length;i++) {

            int b = (0xFF & md5Data[i]);

            if (b <= 0xF) {

                uniqueId+="0";

            }

            uniqueId+=Integer.toHexString(b);

        }

        return  uniqueId.toUpperCase();

    }

[END]

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约