前言
最近在對介面的時候 根據后臺要求生成設備唯一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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/286791.html
標籤:其他
上一篇:Android11中相機的變化
