Why is "using getString () to get device identifiers" not recommended?

I am trying to get my device id in Logcat, but Android Studio gives me this warning.

using gettring to get device identifiers is not recommended

String deviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID); 

Why is this not recommended?

+14
source share
2 answers

The full warning reads:

The use of these device identifiers is not recommended, except in cases of the prevention of high-value fraud and complex cases of using telephony. For advertising scenarios, use AdvertisingIdClient $ Info # getId, and for analytics use InstanceId # getId.

Additional information: https://developer.android.com/training/articles/user-data-ids.html

I think Android does not recommend using this value, as it is a permanent unique identifier that can be used to track your user, and he or she cannot change this identifier or prevent you from reading it. Other parameters indicated in the warning correspond to your wishes. This is why Android recommends you use this.

It really depends on what you are going to do with this identifier. Privacy is very important these days.

Also check this out:

... you must comply with the "Opt-out of interest-based advertising" or "Opt-out of personalizing ads" setting. If the user has enabled this option, you cannot use the advertising identifier to create user profiles for advertising purposes or to target users using personalized advertising. Allowed actions include contextual advertising, frequency capping, conversion tracking, reporting and security, and fraud detection.

Source: https://developer.android.com/training/articles/user-data-ids.html

+11
source

As Giovanni says, change your code to:

 String deviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), InstanceID.getInstance(getApplicationContext()).getId()); 

or

 String deviceID = Settings.Secure.getString(getApplicationContext().getContentResolver(), AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()).getId()); 

如果 在 中国 大陆 发布 此 app, 请勿 使用 以上 方法, 可能 导致 NullPointException, 因为 大部分 设备 都 没有 谷 歌 Service, 无法 获取 此 id. 请 使用

 android.provider.Settings.Secure.ANDROID_ID 

然后 加上 lint 屏蔽

 @SuppressLint("HardwareIds") 

In English: if your application is to be released in China, the above method may result in a NullPointException.Because most devices in China do not have a Google service. Try as follows:

 android.provider.Settings.Secure.ANDROID_ID 

and add suppressLint:

 @SuppressLint("HardwareIds") 
-4
source

Source: https://habr.com/ru/post/1273914/


All Articles