Android 获取设备号
作者:互联网
前言
最近在对接口的时候 根据后台要求生成设备唯一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;
}
标签:mTelephony,Settings,获取,VERSION,Build,context,Android,设备,deviceId 来源: https://blog.csdn.net/Life_s/article/details/117770064