获取手机的设备号

/**
     * 获取手机的设备号.
     * @param context 上下文
     * @return 设备号
     */
    @SuppressLint("HardwareIds")
    public static String getIMEIDeviceId(Context context) {
    
    

        String deviceId;
        //如果sdk版本大于等于29
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    
    
            deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else {
    
    
            final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    
                if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
    
    
                    return "";
                }
            }
            assert mTelephony != null;
            if (mTelephony.getDeviceId() != null) {
    
    
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    
    
                    deviceId = mTelephony.getImei();
                } else {
    
    
                    deviceId = mTelephony.getDeviceId();
                }
            } else {
    
    
                deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
            }
        }

        return deviceId;
    }

猜你喜欢

转载自blog.csdn.net/ayibosi/article/details/114275933