现在随着 Android 10 的权限和安全性更新,我们无法访问用户设备的设备 ID 和 IMEI 号码,但我需要一些设备的唯一标识来跟踪用户。
要求是我们希望每个电话只能登录一次/受限制。
现在随着 Android 10 的权限和安全性更新,我们无法访问用户设备的设备 ID 和 IMEI 号码,但我需要一些设备的唯一标识来跟踪用户。
要求是我们希望每个电话只能登录一次/受限制。
Android 10限制开发者访问IMEI号码。
你可以通过获取软件ID来获得备用解决方案。你可以将软件ID作为唯一ID。请查看下面的代码,这是我在应用程序中使用的代码。
public static String getDeviceId(Context context) {
String deviceId;
if (android.os.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 (mTelephony.getDeviceId() != null) {
deviceId = mTelephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
}
return deviceId;
}
自API 26级别起,getDeviceId()已被弃用。
"READ_PRIVILEGE_PHONE_STATE"仅可由最佳实践建议访问,您应该“避免使用硬件标识符”作为唯一标识符。您可以使用来自Firebase的实例ID,例如FirebaseInstanceId.getInstance().getId();
public static String getDeviceId(Context context) {
String deviceId;
if (android.os.Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
} else {
deviceId =FirebaseInstanceId.getInstance().getId();
}
return deviceId;
}
在 Android 10 及以上版本中,请使用 FirebaseInstanceId.getInstance().getId();
这对我有效 //导入android.provider.Settings
val mId = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)
自 Android 10 及以后,序列号和IMEI号已被弃用, 因此我们可以使用 READ_PRIVILEGED_PHONE_STATE 获取 Android ID 作为唯一标识符。
如需更多信息,请参阅以下链接。 https://developer.android.com/training/articles/user-data-ids
Android引入了一个新的ID来唯一标识设备,称为Advertisement_Id。您可以从以下代码实现中获取此ID,在您的应用程序类onCreate方法中。
/** Retrieve the Android Advertising Id
*
* The device must be KitKat (4.4)+
* This method must be invoked from a background thread.
*
* */
public static synchronized String getAdId (Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
return null;
}
AdvertisingIdClient.Info idInfo = null;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String advertId = null;
try{
advertId = idInfo.getId();
}catch (NullPointerException e){
e.printStackTrace();
}
return advertId;
}
针对 Kotlin
fun getAdId() {
//Background Task
AsyncTask.execute {
var adInfo: AdvertisingIdClient.Info? = null
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)
if(adInfo!=null){
val id = adInfo!!.getId()
val isLAT = adInfo!!.isLimitAdTrackingEnabled()
PersistData.setStringData(applicationContext, AppConstant.advertId, id)
val advertId = PersistData.getStringData(applicationContext, AppConstant.advertId)
}
} catch (e: IOException) {
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
} catch (e: GooglePlayServicesAvailabilityException) {
// Encountered a recoverable error connecting to Google Play services.
} catch (e: GooglePlayServicesNotAvailableException) {
// Google Play services is not available entirely.
}
}
}