最近在对接口的时候 根据后台要求生成设备唯一id 作为key
android 获取设备号比较简单 这里记录一下
/** * 获取设备号 * @param context * @return */ public static String getDeviceId(Context context) { String deviceId; if (Build.VERSION.SDK_INT >= 29) { 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; }