/**
* 获取手机的设备号.
* @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;
}
获取手机的设备号
最新推荐文章于 2023-11-21 18:08:59 发布