private fun deviceId(): String? {
val deviceIdKey = "makeDeviceID"
val sharedPreferences = getSharedPreferences("AppDate", 0)
var deviceId = sharedPreferences.getString(deviceIdKey, null)
if (deviceId == null) {
deviceId = retrieveDeviceId()
if (deviceId == null) {
deviceId = retrieveAndroidIdOrGenerateUUID()
}
val editor = sharedPreferences.edit()
editor.putString(deviceIdKey, deviceId)
editor.commit()
}
return deviceId
}
private fun retrieveDeviceId(): String? {
return try {
val telephonyManager =
this.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
if (Build.VERSION.SDK_INT < 26) {
telephonyManager.deviceId
} else {
telephonyManager.imei
}
} catch (e: Exception) {
// Handle exception (log if necessary)
null
}
}
private fun retrieveAndroidIdOrGenerateUUID(): String? {
try {
val androidId = Settings.Secure.getString(
this.contentResolver,
Settings.Secure.ANDROID_ID
)
if (androidId != null && androidId.isNotEmpty()) {
return androidId
}
} catch (e: Exception) {
// Handle exception (log if necessary)
}
return UUID.randomUUID().toString().replace("-".toRegex(), "")
}
'Android' 카테고리의 다른 글
[안드로이드] 에뮬레이터 네트워킹(localhost 테스트) (0) | 2022.12.19 |
---|---|
[안드로이드]EncryptedSharedPreferences 사용 방법 (0) | 2022.12.08 |
[안드로이드] TextClock 사용법 (0) | 2022.12.02 |
[안드로이드] Execution failed for task ':app:validateSigningDebug' 오류(debug.keystore not found ) (0) | 2022.11.29 |
[안드로이드] React-Native Error: error:0308010C:digital envelope routines::unsupported (0) | 2022.11.29 |